Skip to content

Commit faf597f

Browse files
committed
feat: non-overlapping-intervals
1 parent a74d56c commit faf597f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/non-overlapping-intervals/">week12-3. non-overlapping-intervals</a>
3+
* <li>Description: return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</li>
4+
* <li>Topics: Array, Dynamic Programming, Greedy, Sorting </li>
5+
* <li>Time Complexity: O(N), Runtime 45ms </li>
6+
* <li>Space Complexity: O(1), Memory 73.45MB </li>
7+
* <li>Note : refer to the answer </li>
8+
*/
9+
class Solution {
10+
public int eraseOverlapIntervals(int[][] intervals) {
11+
Arrays.sort(intervals, (i1, i2) -> i1[1] - i2[1]);
12+
13+
int skip = 0;
14+
int end = Integer.MIN_VALUE;
15+
16+
for (int[] interval : intervals) {
17+
if (interval[0] < end) {
18+
skip++;
19+
} else {
20+
end = interval[1];
21+
}
22+
}
23+
24+
return skip;
25+
}
26+
}

0 commit comments

Comments
 (0)