Skip to content

Commit f277d32

Browse files
committed
Non Overlapping Intervals
1 parent 4c325d2 commit f277d32

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n log n)
2+
// in order to order the given intervals array
3+
// SC: O(1)
4+
// only constant space necessary
5+
class Solution {
6+
public int eraseOverlapIntervals(int[][] intervals) {
7+
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[1], o2[1]));
8+
9+
int output = 0;
10+
int end = intervals[0][1];
11+
12+
for (int i = 1; i < intervals.length; i++) {
13+
int[] currentInterval = intervals[i];
14+
15+
if (currentInterval[0] < end) output += 1;
16+
else end = currentInterval[1];
17+
}
18+
19+
return output;
20+
}
21+
}

0 commit comments

Comments
 (0)