/**
* Problem: LeetCode 1288 - Remove Covered Intervals
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 1288 - Remove Covered Intervals
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int removeCoveredIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> {
if (a[0] == b[0]) {
return b[1] - a[1];
}
return a[0] - b[0];
});
int count = 0;
int maxEnd = 0;
for (int[] interval : intervals) {
if (interval[1] > maxEnd) {
count++;
maxEnd = interval[1];
}
}
return count;
}
}
"""
Problem: LeetCode 1288 - Remove Covered Intervals
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
# Sort by start ascending, then by end descending
intervals.sort(key=lambda x: (x[0], -x[1]))
count = 0
max_end = 0
for _, r in intervals:
if r > max_end:
count += 1
max_end = r
return count
/**
* Problem: LeetCode 1288 - Remove Covered Intervals
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 1288 - Remove Covered Intervals
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int removeCoveredIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
if (a[0] == b[0]) {
return a[1] > b[1];
}
return a[0] < b[0];
});
int count = 0;
int maxEnd = 0;
for (const auto& interval : intervals) {
if (interval[1] > maxEnd) {
count++;
maxEnd = interval[1];
}
}
return count;
}
};
/**
* Problem: LeetCode 1288 - Remove Covered Intervals
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var removeCoveredIntervals = function(intervals) {
intervals.sort((a, b) => {
if (a[0] === b[0]) {
return b[1] - a[1];
}
return a[0] - b[0];
});
let count = 0;
let maxEnd = 0;
for (let i = 0; i < intervals.length; i++) {
if (intervals[i][1] > maxEnd) {
count++;
maxEnd = intervals[i][1];
}
}
return count;
};