Skip to content

Commit a782c88

Browse files
authored
Merge pull request #25 from iamAntimPal/Leetcode-75
Leetcode 75
2 parents a7e9a99 + ca9a3f3 commit a782c88

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findMinArrowShots(self, points: List[List[int]]) -> int:
3+
ans, last = 0, -inf
4+
for a, b in sorted(points, key=lambda x: x[1]):
5+
if a > last:
6+
ans += 1
7+
last = b
8+
return ans
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: Antim
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+
## Description
16+
17+
<!-- description:start -->
18+
19+
<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>
20+
21+
<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>
22+
23+
<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>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> points = [[10,16],[2,8],[1,6],[7,12]]
30+
<strong>Output:</strong> 2
31+
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
32+
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
33+
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
34+
</pre>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> points = [[1,2],[3,4],[5,6],[7,8]]
40+
<strong>Output:</strong> 4
41+
<strong>Explanation:</strong> One arrow needs to be shot for each balloon for a total of 4 arrows.
42+
</pre>
43+
44+
<p><strong class="example">Example 3:</strong></p>
45+
46+
<pre>
47+
<strong>Input:</strong> points = [[1,2],[2,3],[3,4],[4,5]]
48+
<strong>Output:</strong> 2
49+
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
50+
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
51+
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= points.length &lt;= 10<sup>5</sup></code></li>
59+
<li><code>points[i].length == 2</code></li>
60+
<li><code>-2<sup>31</sup> &lt;= x<sub>start</sub> &lt; x<sub>end</sub> &lt;= 2<sup>31</sup> - 1</code></li>
61+
</ul>
62+
63+
<!-- description:end -->
64+
65+
## Solutions
66+
67+
<!-- solution:start -->
68+
69+
### Solution 1
70+
71+
<!-- tabs:start -->
72+
73+
#### Python3
74+
75+
```python
76+
class Solution:
77+
def findMinArrowShots(self, points: List[List[int]]) -> int:
78+
ans, last = 0, -inf
79+
for a, b in sorted(points, key=lambda x: x[1]):
80+
if a > last:
81+
ans += 1
82+
last = b
83+
return ans
84+
```
85+
86+
#### Java
87+
88+
```java
89+
class Solution {
90+
public int findMinArrowShots(int[][] points) {
91+
// 直接 a[1] - b[1] 可能会溢出
92+
Arrays.sort(points, Comparator.comparingInt(a -> a[1]));
93+
int ans = 0;
94+
long last = -(1L << 60);
95+
for (var p : points) {
96+
int a = p[0], b = p[1];
97+
if (a > last) {
98+
++ans;
99+
last = b;
100+
}
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
107+
#### C++
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int findMinArrowShots(vector<vector<int>>& points) {
113+
sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b) {
114+
return a[1] < b[1];
115+
});
116+
int ans = 0;
117+
long long last = -(1LL << 60);
118+
for (auto& p : points) {
119+
int a = p[0], b = p[1];
120+
if (a > last) {
121+
++ans;
122+
last = b;
123+
}
124+
}
125+
return ans;
126+
}
127+
};
128+
```
129+
130+
131+
<!-- tabs:end -->
132+
133+
<!-- solution:end -->
134+
135+
<!-- problem:end -->

0 commit comments

Comments
 (0)