Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 51af125

Browse files
committedFeb 27, 2025
DaleStudy#279 Non Overlapping Intervals
1 parent 3b0cd47 commit 51af125

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
# Time Complexity: O(nlogn)
3+
- 정렬 : O(nlogn)
4+
- for-loop : O(n)
5+
# Space Complexity: O(1)
6+
*/
7+
class Solution {
8+
public int eraseOverlapIntervals(int[][] intervals) {
9+
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
10+
11+
int cnt = 0;
12+
int prev_end = intervals[0][1];
13+
14+
for (int i = 1; i < intervals.length; i++) {
15+
if (prev_end > intervals[i][0]) {
16+
cnt++;
17+
} else {
18+
prev_end = intervals[i][1];
19+
}
20+
}
21+
22+
return cnt;
23+
}
24+
}

0 commit comments

Comments
 (0)