MrainW's Home

All things come to those who wait!

0%

LeetCode 1288. Remove Covered Intervals

Question

Given an array intervals where intervals[i] = [li, ri] represent the interval [li, ri), remove all intervals that are covered by another interval in the list.

The interval [a, b) is covered by the interval [c, d) if and only if c <= a and b <= d.

Return the number of remaining intervals.

https://leetcode.com/problems/remove-covered-intervals/

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

Complexity:

Time complexity: O( nlogn)

Space complexity: O(1)

Welcome to my other publishing channels