Skip to content

Commit 1e573e3

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent f1f6d7b commit 1e573e3

File tree

1 file changed

+189
-0
lines changed
  • Solution/452. Minimum Number of Arrows to Burst Balloons

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0452.Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/README_EN.md
5+
tags:
6+
- Greedy
7+
- Array
8+
- Sorting
9+
---
10+
11+
<!-- problem:start -->
12+
13+
# [452. Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons)
14+
15+
[中文文档](/solution/0400-0499/0452.Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/README.md)
16+
17+
## Description
18+
19+
<!-- description:start -->
20+
21+
<p>There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array <code>points</code> where <code>points[i] = [x<sub>start</sub>, x<sub>end</sub>]</code> denotes a balloon whose <strong>horizontal diameter</strong> stretches between <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code>. You do not know the exact y-coordinates of the balloons.</p>
22+
23+
<p>Arrows can be shot up <strong>directly vertically</strong> (in the positive y-direction) from different points along the x-axis. A balloon with <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code> is <strong>burst</strong> by an arrow shot at <code>x</code> if <code>x<sub>start</sub> &lt;= x &lt;= x<sub>end</sub></code>. There is <strong>no limit</strong> to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.</p>
24+
25+
<p>Given the array <code>points</code>, return <em>the <strong>minimum</strong> number of arrows that must be shot to burst all balloons</em>.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> points = [[10,16],[2,8],[1,6],[7,12]]
32+
<strong>Output:</strong> 2
33+
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
34+
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
35+
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
36+
</pre>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> points = [[1,2],[3,4],[5,6],[7,8]]
42+
<strong>Output:</strong> 4
43+
<strong>Explanation:</strong> One arrow needs to be shot for each balloon for a total of 4 arrows.
44+
</pre>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<pre>
49+
<strong>Input:</strong> points = [[1,2],[2,3],[3,4],[4,5]]
50+
<strong>Output:</strong> 2
51+
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
52+
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
53+
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= points.length &lt;= 10<sup>5</sup></code></li>
61+
<li><code>points[i].length == 2</code></li>
62+
<li><code>-2<sup>31</sup> &lt;= x<sub>start</sub> &lt; x<sub>end</sub> &lt;= 2<sup>31</sup> - 1</code></li>
63+
</ul>
64+
65+
<!-- description:end -->
66+
67+
## Solutions
68+
69+
<!-- solution:start -->
70+
71+
### Solution 1
72+
73+
<!-- tabs:start -->
74+
75+
#### Python3
76+
77+
```python
78+
class Solution:
79+
def findMinArrowShots(self, points: List[List[int]]) -> int:
80+
ans, last = 0, -inf
81+
for a, b in sorted(points, key=lambda x: x[1]):
82+
if a > last:
83+
ans += 1
84+
last = b
85+
return ans
86+
```
87+
88+
#### Java
89+
90+
```java
91+
class Solution {
92+
public int findMinArrowShots(int[][] points) {
93+
// 直接 a[1] - b[1] 可能会溢出
94+
Arrays.sort(points, Comparator.comparingInt(a -> a[1]));
95+
int ans = 0;
96+
long last = -(1L << 60);
97+
for (var p : points) {
98+
int a = p[0], b = p[1];
99+
if (a > last) {
100+
++ans;
101+
last = b;
102+
}
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
#### C++
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int findMinArrowShots(vector<vector<int>>& points) {
115+
sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b) {
116+
return a[1] < b[1];
117+
});
118+
int ans = 0;
119+
long long last = -(1LL << 60);
120+
for (auto& p : points) {
121+
int a = p[0], b = p[1];
122+
if (a > last) {
123+
++ans;
124+
last = b;
125+
}
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
132+
#### Go
133+
134+
```go
135+
func findMinArrowShots(points [][]int) (ans int) {
136+
sort.Slice(points, func(i, j int) bool { return points[i][1] < points[j][1] })
137+
last := -(1 << 60)
138+
for _, p := range points {
139+
a, b := p[0], p[1]
140+
if a > last {
141+
ans++
142+
last = b
143+
}
144+
}
145+
return
146+
}
147+
```
148+
149+
#### TypeScript
150+
151+
```ts
152+
function findMinArrowShots(points: number[][]): number {
153+
points.sort((a, b) => a[1] - b[1]);
154+
let ans = 0;
155+
let last = -Infinity;
156+
for (const [a, b] of points) {
157+
if (last < a) {
158+
ans++;
159+
last = b;
160+
}
161+
}
162+
return ans;
163+
}
164+
```
165+
166+
#### C#
167+
168+
```cs
169+
public class Solution {
170+
public int FindMinArrowShots(int[][] points) {
171+
Array.Sort(points, (a, b) => a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0);
172+
int ans = 0;
173+
long last = long.MinValue;
174+
foreach (var point in points) {
175+
if (point[0] > last) {
176+
++ans;
177+
last = point[1];
178+
}
179+
}
180+
return ans;
181+
}
182+
}
183+
```
184+
185+
<!-- tabs:end -->
186+
187+
<!-- solution:end -->
188+
189+
<!-- problem:end -->

0 commit comments

Comments
 (0)