Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
ans, last = 0, -inf
for a, b in sorted(points, key=lambda x: x[1]):
if a > last:
ans += 1
last = b
return ans
135 changes: 135 additions & 0 deletions Solution/452. Minimum Number of Arrows to Burst Balloons/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
comments: true
difficulty: Medium
edit_url: Antim
tags:
- Greedy
- Array
- Sorting
---

<!-- problem:start -->

# [452. Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons)

## Description

<!-- description:start -->

<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>

<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>

<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> points = [[10,16],[2,8],[1,6],[7,12]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> points = [[1,2],[3,4],[5,6],[7,8]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One arrow needs to be shot for each balloon for a total of 4 arrows.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> points = [[1,2],[2,3],[3,4],[4,5]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= points.length &lt;= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-2<sup>31</sup> &lt;= x<sub>start</sub> &lt; x<sub>end</sub> &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
ans, last = 0, -inf
for a, b in sorted(points, key=lambda x: x[1]):
if a > last:
ans += 1
last = b
return ans
```

#### Java

```java
class Solution {
public int findMinArrowShots(int[][] points) {
// 直接 a[1] - b[1] 可能会溢出
Arrays.sort(points, Comparator.comparingInt(a -> a[1]));
int ans = 0;
long last = -(1L << 60);
for (var p : points) {
int a = p[0], b = p[1];
if (a > last) {
++ans;
last = b;
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b) {
return a[1] < b[1];
});
int ans = 0;
long long last = -(1LL << 60);
for (auto& p : points) {
int a = p[0], b = p[1];
if (a > last) {
++ans;
last = b;
}
}
return ans;
}
};
```


<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->