Skip to content

Commit e1bf8a3

Browse files
solve #2848: Points That Intersect With Cars in java
1 parent 0dadeaa commit e1bf8a3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@
853853
| 2833 | [Furthest Point From Origin](https://leetcode.com/problems/furthest-point-from-origin) | [![Java](assets/java.png)](src/FurthestPointFromOrigin.java) | |
854854
| 2839 | [Check if Strings Can be Made Equal With Operations I](https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i) | [![Java](assets/java.png)](src/CheckIfStringsCanBeMadeEqualWithOperationsI.java) | |
855855
| 2843 | [Count Symmetric Integers](https://leetcode.com/problems/count-symmetric-integers) | [![Java](assets/java.png)](src/CountSymmetricIntegers.java) | |
856-
| 2848 | [Points That Intersect With Cars](https://leetcode.com/problems/points-that-intersect-with-cars) | | |
856+
| 2848 | [Points That Intersect With Cars](https://leetcode.com/problems/points-that-intersect-with-cars) | [![Java](assets/java.png)](src/PointsThatIntersectWithCars.java) | |
857857
| 2855 | [Minimum Right Shifts to Sort the Array](https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array) | | |
858858
| 2859 | [Sum of Values at Indices With K Set Bits](https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits) | | |
859859
| 2833 | [Furthest Point From Origin](https://leetcode.com/problems/furthest-point-from-origin) | | |

src/PointsThatIntersectWithCars.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// https://leetcode.com/problems/points-that-intersect-with-cars
2+
// T: O(|nums| + |cars|)
3+
// S: O(|cars|)
4+
5+
import java.util.List;
6+
7+
public class PointsThatIntersectWithCars {
8+
public int numberOfPoints(List<List<Integer>> nums) {
9+
final int[] points = new int[101];
10+
int count = 0;
11+
12+
for (final List<Integer> car : nums) {
13+
points[car.get(0) - 1]++;
14+
points[car.get(1)]--;
15+
}
16+
17+
for (int i = 0, overlap = 0 ; i < points.length ; i++) {
18+
overlap += points[i];
19+
if (overlap > 0) {
20+
count++;
21+
}
22+
}
23+
return count;
24+
}
25+
}

0 commit comments

Comments
 (0)