-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBurstBalloons
30 lines (24 loc) · 976 Bytes
/
BurstBalloons
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Author: Sayed Mahmudul Alam
// Source: LeetCode-452: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/
// Solution Ref: https://www.youtube.com/watch?v=uC0aDIhwGdE&t=431s&ab_channel=NareshGupta
// Example Input: points = [[10,16],[2,8],[1,6],[7,12]]
// Output: 2
class Solution {
func findMinArrowShots(_ points: [[Int]]) -> Int {
// if input array is empty then no arrow needed
guard !points.isEmpty else { return 0 }
// sort the array in descending order
let sorted = points.sorted { $0[1] < $1[1] }
var arrow = 1
var end = sorted[0][1]
// check if the start of a point is greater then the `end`
// if it's greater then will need another array and 'end' should be updated
for i in 1..<points.count {
if sorted[i][0] > end {
arrow += 1
end = sorted[i][1]
}
}
return arrow
}
}