-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode435.java
More file actions
66 lines (58 loc) · 2.25 KB
/
LeetCode435.java
File metadata and controls
66 lines (58 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Arrays;
import java.util.Comparator;
public class LeetCode435 {
public static void main(String[] args) {
// 输入: intervals = [[1,2],[2,3],[3,4],[1,3]]
// 输出: 1
System.out.println(
new Solution435().eraseOverlapIntervals(new int[][] { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 1, 3 } }));
// 输入: intervals = [ [1,2], [1,2], [1,2] ]
// 输出: 2
System.out.println(
new Solution435().eraseOverlapIntervals(new int[][] { { 1, 2 }, { 1, 2 }, {
1, 2 } }));
// 输入: intervals = [ [1,2], [2,3] ]
// 输出: 0
System.out.println(
new Solution435().eraseOverlapIntervals(new int[][] { { 1, 2 }, { 2, 3 }
}));
// 输入: intervals = [[1,100],[11,22],[1,11],[2,12]]
// 输出: 2
System.out.println(
new Solution435()
.eraseOverlapIntervals(new int[][] { { 1, 100 }, { 11, 22 }, { 1, 11 }, { 2,
12 } }));
// 输入: intervals =
// [[-52,31],[-73,-26],[82,97],[-65,-11],[-62,-49],[95,99],[58,95],[-31,49],[66,98],[-63,2],[30,47],[-40,-26]]
// 输出: 7
System.out.println(
new Solution435().eraseOverlapIntervals(
new int[][] { { -52, 31 }, { -73, -26 }, { 82, 97 }, { -65, -11 }, { -62, -49
}, { 95, 99 },
{ 58, 95 }, { -31, 49 }, { 66, 98 }, { -63, 2 }, { 30, 47 }, { -40, -26 }
}));
}
}
class Solution435 {
// NOTE: 贪心算法更新右边界
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length == 0) {
return 0;
}
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] interval1, int[] interval2) {
return interval1[1] - interval2[1];
}
});
int n = intervals.length;
int right = intervals[0][1];
int ans = 1;
for (int i = 1; i < n; ++i) {
if (intervals[i][0] >= right) {
++ans;
right = intervals[i][1];
}
}
return n - ans;
}
}