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

Commit8b37fb1

Browse files
authored
Merge pull requestneetcode-gh#15 from ProgrammingPete/main
Added 435 Non-Overlapping Intervals Java Solution
2 parentsbd6cf3c +80221a4 commit8b37fb1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
publicclassSolution {
2+
publicinteraseOverlapIntervals(int[][]intervals) {
3+
intintervalsRemoved =0;
4+
5+
Arrays.sort(intervals, (arr1,arr2) ->Integer.compare(arr1[0],arr2[0]));
6+
7+
int[]intervalFirst =intervals[0];
8+
9+
for(inti =1;i <intervals.length;i++){
10+
if(firstIntervalwithinSecond(intervalFirst,intervals[i])){
11+
//mark first interval to be removed
12+
intervalsRemoved++;
13+
// determine which interval to remove
14+
//remove the interval that ends last
15+
if(intervalFirst[1] >intervals[i][1]){
16+
intervalFirst =intervals[i];
17+
}
18+
}else {
19+
intervalFirst =intervals[i];
20+
}
21+
}
22+
returnintervalsRemoved;
23+
}
24+
25+
publicbooleanfirstIntervalwithinSecond(int[]intervalFirst,int[]intervalSecond){
26+
returnintervalSecond[0] <intervalFirst[1];
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp