Problem : https://leetcode.com/problems/remove-covered-intervals/
Steps:
- Sort intervals by its beginning point in ascending order. If beginning point are the same, sort by ending point in descending order.
- Iterate all intervals. If one interval cannot extend current ending point to further position, then it is covered and can be removed.
Time complexity = O ( N * Log(N) ) + O ( N )
class Solution {
public int removeCoveredIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]);
int result = intervals.length;
int rightSoFar = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][1] <= rightSoFar) {
result -= 1;
} else {
rightSoFar = intervals[i][1];
}
}
return result;
}
}