mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
swift: add «452. Minimum Number of Arrows to Burst Balloons»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
c02a481db2
commit
6af5eb1c67
1 changed files with 19 additions and 0 deletions
19
swift/minimum-number-of-arrows-to-burst-balloons.swift
Normal file
19
swift/minimum-number-of-arrows-to-burst-balloons.swift
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
class Solution {
|
||||||
|
func findMinArrowShots(_ points: [[Int]]) -> Int {
|
||||||
|
let points = points.sorted(by: { a, b in return a[0] < b[0] })
|
||||||
|
|
||||||
|
var usedArrows = 1
|
||||||
|
var maxX = points[0][1]
|
||||||
|
|
||||||
|
for point in points.dropFirst() {
|
||||||
|
if point[0] <= maxX {
|
||||||
|
maxX = min(maxX, point[1])
|
||||||
|
} else {
|
||||||
|
maxX = point[1]
|
||||||
|
usedArrows += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return usedArrows
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue