Skip to content

Commit b7bcaa0

Browse files
authored
Added task 435.
1 parent 0edb00f commit b7bcaa0

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0435_non_overlapping_intervals;
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Greedy
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
/**
9+
* This is sorting my starting time, the key here is that we'll want to update end time when an
10+
* erasure is needed: we use the smaller end time instead of the bigger one which is more likely
11+
* to overlap with others.
12+
*/
13+
public int eraseOverlapIntervals(int[][] intervals) {
14+
Arrays.sort(intervals, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
15+
int erasures = 0;
16+
int end = intervals[0][1];
17+
for (int i = 1; i < intervals.length; i++) {
18+
if (intervals[i][0] < end) {
19+
erasures++;
20+
end = Math.min(end, intervals[i][1]);
21+
} else {
22+
end = intervals[i][1];
23+
}
24+
}
25+
return erasures;
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
435\. Non-overlapping Intervals
2+
3+
Medium
4+
5+
Given an array of intervals `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, return _the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping_.
6+
7+
**Example 1:**
8+
9+
**Input:** intervals = [[1,2],[2,3],[3,4],[1,3]]
10+
11+
**Output:** 1
12+
13+
**Explanation:** [1,3] can be removed and the rest of the intervals are non-overlapping.
14+
15+
**Example 2:**
16+
17+
**Input:** intervals = [[1,2],[1,2],[1,2]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** You need to remove two [1,2] to make the rest of the intervals non-overlapping.
22+
23+
**Example 3:**
24+
25+
**Input:** intervals = [[1,2],[2,3]]
26+
27+
**Output:** 0
28+
29+
**Explanation:** You don't need to remove any of the intervals since they're already non-overlapping.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= intervals.length <= 10<sup>5</sup></code>
34+
* `intervals[i].length == 2`
35+
* <code>-5 * 10<sup>4</sup> <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>4</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0435_non_overlapping_intervals;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void eraseOverlapIntervals() {
11+
assertThat(
12+
new Solution().eraseOverlapIntervals(new int[][] {{1, 2}, {2, 3}, {3, 4}, {1, 3}}),
13+
equalTo(1));
14+
}
15+
16+
@Test
17+
void eraseOverlapIntervals2() {
18+
assertThat(
19+
new Solution().eraseOverlapIntervals(new int[][] {{1, 2}, {1, 2}, {1, 2}}),
20+
equalTo(2));
21+
}
22+
23+
@Test
24+
void eraseOverlapIntervals3() {
25+
assertThat(new Solution().eraseOverlapIntervals(new int[][] {{1, 2}, {2, 3}}), equalTo(0));
26+
}
27+
}

0 commit comments

Comments
 (0)