Remove Covered Intervals
Given a list of intervals, remove all intervals that are covered by another interval in the list. Interval [a,b)
is covered by interval [c,d)
if and only if c <= a
and b <= d
.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Constraints:
1 <= intervals.length <= 1000
0 <= intervals[i][0] < intervals[i][1] <= 10^5
intervals[i] != intervals[j]
for alli != j
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;
int currentEnd = 0;
// By sorting we make sure that interval before will definately will have
// smaller start time therefore we only need to take care of end time
for (int[] interval : intervals) {
if (currentEnd < interval[1]) {
count++;
currentEnd = interval[1];
}
}
return count;
}
}
PreviousMaximum Side Length of a Square with Sum Less than or Equal to ThresholdNextDistant Barcodes
Last updated