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

Commit45d9170

Browse files
add a solution for 435
1 parent9ca9c85 commit45d9170

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,24 @@ public int eraseOverlapIntervals(int[][] intervals) {
2727
}
2828
}
2929

30+
publicstaticclassSolution2 {
31+
/**
32+
* This is sorting my starting time, the key here is that we'll want to update end time when an erasure is needed:
33+
* we use the smaller end time instead of the bigger one which is more likely to overlap with others.
34+
*/
35+
publicinteraseOverlapIntervals(int[][]intervals) {
36+
Arrays.sort(intervals, (a,b) ->a[0] !=b[0] ?a[0] -b[0] :a[1] -b[1]);
37+
interasures =0;
38+
intend =intervals[0][1];
39+
for (inti =1;i <intervals.length;i++) {
40+
if (intervals[i][0] <end) {
41+
erasures++;
42+
end =Math.min(end,intervals[i][1]);
43+
}else {
44+
end =intervals[i][1];
45+
}
46+
}
47+
returnerasures;
48+
}
49+
}
3050
}

‎src/test/java/com/fishercoder/_435Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@
99

1010
publicclass_435Test {
1111
privatestatic_435.Solution1solution1;
12+
privatestatic_435.Solution2solution2;
1213

1314
@BeforeClass
1415
publicstaticvoidsetup() {
1516
solution1 =new_435.Solution1();
17+
solution2 =new_435.Solution2();
1618
}
1719

1820
@Test
1921
publicvoidtest1() {
2022
int[][]intervals =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[2,3],[3,4],[1,3]");
2123
assertEquals(1,solution1.eraseOverlapIntervals(intervals));
24+
assertEquals(1,solution2.eraseOverlapIntervals(intervals));
25+
}
26+
27+
@Test
28+
publicvoidtest2() {
29+
int[][]intervals =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[-52,31],[-73,-26],[82,97],[-65,-11],[-62,-49],[95,99],[58,95],[-31,49],[66,98],[-63,2],[30,47],[-40,-26]");
30+
assertEquals(7,solution1.eraseOverlapIntervals(intervals));
31+
assertEquals(7,solution2.eraseOverlapIntervals(intervals));
2232
}
2333

2434
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp