MrainW's Home

All things come to those who wait!

0%

LeetCode 435. Non-overlapping Intervals

Question

Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

https://leetcode.com/problems/non-overlapping-intervals/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length == 0) return 0;
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
int count = 0, end = Integer.MIN_VALUE;
for (int[] cur : intervals){
if (end <= cur[0]) end = cur[1];
else count++;
}
return count;
}
}

Complexity:

Time complexity: O( nlogn)

Space complexity: O(1)

Welcome to my other publishing channels