Skip to content

Commit cbdf34b

Browse files
authored
Create Points That Intersect With Cars.java
1 parent 6442928 commit cbdf34b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int numberOfPoints(List<List<Integer>> coordinates) {
3+
coordinates.sort(Comparator.comparingInt((List<Integer> o) -> o.get(0)).thenComparingInt(o -> o.get(1)));
4+
int points = 0;
5+
int intervalStart = -1;
6+
int intervalEnd = -1;
7+
for (List<Integer> coordinate : coordinates) {
8+
if (intervalStart == -1) {
9+
intervalStart = coordinate.get(0);
10+
} else {
11+
if (intervalEnd < coordinate.get(0)) {
12+
points += intervalEnd - intervalStart + 1;
13+
intervalStart = coordinate.get(0);
14+
}
15+
}
16+
intervalEnd = Math.max(intervalEnd, coordinate.get(1));
17+
}
18+
return points + intervalEnd - intervalStart + 1;
19+
}
20+
}

0 commit comments

Comments
 (0)