swift: add «435. Non-overlapping Intervals»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
6af5eb1c67
commit
79c92c471e
1 changed files with 19 additions and 0 deletions
19
swift/non-overlapping-intervals.swift
Normal file
19
swift/non-overlapping-intervals.swift
Normal file
|
@ -0,0 +1,19 @@
|
|||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue