1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/swift/non-overlapping-intervals.swift
Matej Focko 79c92c471e
swift: add «435. Non-overlapping Intervals»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-02 15:32:53 +02:00

19 lines
455 B
Swift

class Solution {
func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int {
let intervals = intervals
.map { ($0[0], $0[1]) }
.sorted { l, r in l.1 < r.1 }
var lastEnd: Int = .min
var erased = 0
for (start, end) in intervals {
if start >= lastEnd {
lastEnd = end
} else {
erased += 1
}
}
return erased
}
}