-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path452. Minimum Number of Arrows to Burst Balloons.java
59 lines (43 loc) · 1.31 KB
/
452. Minimum Number of Arrows to Burst Balloons.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Leetcode - Solve today's Daily Challenge to refresh your streak!
452. Minimum Number of Arrows to Burst Balloons
JAVA----------------------------------------------------------
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> a[1] - b[1]);
int ans = 1;
int arrowX = points[0][1];
for (int i = 1; i < points.length; ++i)
if (points[i][0] > arrowX) {
arrowX = points[i][1];
++ans;
}
return ans;
}
}
Python--------------------------------------------------------
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
ans = 0
arrowX = -math.inf
for point in sorted(points, key=lambda x: x[1]):
if point[0] > arrowX:
ans += 1
arrowX = point[1]
return ans
C++--------------------------------------------------------
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
ranges::sort(points,
[](const auto& a, const auto& b) { return a[1] < b[1]; });
int ans = 1;
int arrowX = points[0][1];
for (int i = 1; i < points.size(); ++i)
if (points[i][0] > arrowX) {
arrowX = points[i][1];
++ans;
}
return ans;
}
};
--------------------------------------------------------------