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

Commit 5e960b4

Browse files
Update 0435.无重叠区间.md
First java code not working, I test in the IntellJ with jdk 11, the Arrays.sort is not right ( doesn't make the left edge sort from small to big ), so the non overlapping area count fail. I fix it. Sort by end first, if end is same, sort by start
1 parent 6afccb6 commit 5e960b4

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

‎problems/0435.无重叠区间.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,27 @@ Java:
186186
class Solution {
187187
public int eraseOverlapIntervals(int[][] intervals) {
188188
if (intervals.length < 2) return 0;
189+
189190
Arrays.sort(intervals, new Comparator<int[]>() {
190191
@Override
191192
public int compare(int[] o1, int[] o2) {
192-
if (o1[0] != o2[0]) {
193+
if (o1[1] != o2[1]) {
193194
return Integer.compare(o1[1],o2[1]);
194195
} else {
195-
return Integer.compare(o2[0],o1[0]);
196+
return Integer.compare(o1[0],o2[0]);
196197
}
197198
}
198199
});
199200

200-
int count = 0;
201+
int count = 1;
201202
int edge = intervals[0][1];
202203
for (int i = 1; i < intervals.length; i++) {
203-
if (intervals[i][0] < edge) {
204-
count++;
205-
} else {
204+
if (edge <= intervals[i][0]){
205+
count ++; //non overlap + 1
206206
edge = intervals[i][1];
207207
}
208208
}
209-
return count;
209+
return intervals.length -count;
210210
}
211211
}
212212
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /