Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd78784b

Browse files
refactor 435
1 parent63d6f6d commitd78784b

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

‎src/main/java/com/fishercoder/solutions/_435.java

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,61 @@
44

55
importjava.util.Arrays;
66
importjava.util.Collections;
7-
importjava.util.Comparator;
87

9-
/**Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
8+
/**
9+
* 435. Non-overlapping Intervals
10+
*
11+
* Given a collection of intervals,
12+
* find the minimum number of intervals you need to remove to make the rest of the
13+
* intervals non-overlapping.
1014
1115
Note:
1216
You may assume the interval's end point is always bigger than its start point.
1317
Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
18+
1419
Example 1:
1520
Input: [ [1,2], [2,3], [3,4], [1,3] ]
16-
1721
Output: 1
18-
1922
Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
23+
2024
Example 2:
2125
Input: [ [1,2], [1,2], [1,2] ]
22-
2326
Output: 2
24-
2527
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
28+
2629
Example 3:
2730
Input: [ [1,2], [2,3] ]
28-
2931
Output: 0
30-
31-
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.*/
32+
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
33+
*/
3234

3335
publicclass_435 {
3436

35-
/**
36-
* References:: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain
37-
* and https://discuss.leetcode.com/topic/65594/java-least-is-most
38-
* Sort the intervals by their end time, if equal, then sort by their start time.
39-
*/
40-
publicstaticinteraseOverlapIntervals(Interval[]intervals) {
41-
Collections.sort(Arrays.asList(intervals),newComparator<Interval>() {
42-
@Override
43-
publicintcompare(Intervalo1,Intervalo2) {
37+
publicstaticclassSolution1 {
38+
/**
39+
* credit:: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain
40+
* and https://discuss.leetcode.com/topic/65594/java-least-is-most
41+
* Sort the intervals by their end time, if equal, then sort by their start time.
42+
*/
43+
publicinteraseOverlapIntervals(Interval[]intervals) {
44+
Collections.sort(Arrays.asList(intervals), (o1,o2) -> {
4445
if (o1.end !=o2.end) {
4546
returno1.end -o2.end;
4647
}else {
4748
returno2.start -o1.start;
4849
}
50+
});
51+
intend =Integer.MIN_VALUE;
52+
intcount =0;
53+
for (Intervalinterval :intervals) {
54+
if (interval.start >=end) {
55+
end =interval.end;
56+
}else {
57+
count++;
58+
}
4959
}
50-
});
51-
intend =Integer.MIN_VALUE;
52-
intcount =0;
53-
for (Intervalinterval :intervals) {
54-
if (interval.start >=end) {
55-
end =interval.end;
56-
}else {
57-
count++;
58-
}
60+
returncount;
5961
}
60-
returncount;
61-
}
62-
63-
publicstaticvoidmain(String...args) {
64-
//[[1,100],[11,22],[1,11],[2,12]]
65-
Intervalinterval1 =newInterval(1,100);
66-
Intervalinterval2 =newInterval(11,22);
67-
Intervalinterval3 =newInterval(1,11);
68-
Intervalinterval4 =newInterval(2,12);
69-
Interval[]intervals =newInterval[]{interval1,interval2,interval3,interval4};
70-
System.out.println(eraseOverlapIntervals(intervals));
7162
}
7263

73-
}
64+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.Interval;
4+
importcom.fishercoder.solutions._435;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_435Test {
11+
privatestatic_435.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_435.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
Intervalinterval1 =newInterval(1,100);
21+
Intervalinterval2 =newInterval(11,22);
22+
Intervalinterval3 =newInterval(1,11);
23+
Intervalinterval4 =newInterval(2,12);
24+
Interval[]intervals =newInterval[]{interval1,interval2,interval3,interval4};
25+
assertEquals(2,solution1.eraseOverlapIntervals(intervals));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp