LeetCode Entry
1288. Remove Covered Intervals
Count non intersecting intervals
1288. Remove Covered Intervals medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1412
Problem TLDR
Count non intersecting intervals
Intuition
Sort by (left, - right) to take more spread intervals first and skip others.
Approach
- can be a single key a*max-b
Complexity
-
Time complexity: \(O(nlogn)\)
-
Space complexity: \(O(1)\)
Code
fun removeCoveredIntervals(iv: Array<IntArray>)= run {
var m = 0; iv.sortedWith(compareBy({it[0]},{-it[1]}))
.count { (l,r) -> r > m.also { m = max(m, r) } }
}
pub fn remove_covered_intervals(mut iv: Vec<Vec<i32>>) -> i32 {
iv.sort_by_key(|i|(i[0],-i[1]));
iv.iter().fold((0,0),|(c,m),i|(c+(i[1]>m)as i32,m.max(i[1]))).0
}
Comments