leet-code/src/main/java/leetcode/editor/cn/MinimumNumberOfArrowsToBurstBalloons.java
2021-08-15 20:54:36 +08:00

91 lines
2.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横
//坐标就足够了。开始坐标总是小于结束坐标。
//
// 一支弓箭可以沿着 x 轴从不同点完全垂直地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstartxend 且满足 xsta
//rt ≤ x ≤ xend则该气球会被引爆。可以射出的弓箭的数量没有限制。 弓箭一旦被射出之后,可以无限地前进。我们想找到使得所有气球全部被引爆,所需的弓箭的
//最小数量。
//
// 给你一个数组 points ,其中 points [i] = [xstart,xend] ,返回引爆所有气球所必须射出的最小弓箭数。
//
//
// 示例 1
//
//
//输入points = [[10,16],[2,8],[1,6],[7,12]]
//输出2
//解释对于该样例x = 6 可以射爆 [2,8],[1,6] 两个气球,以及 x = 11 射爆另外两个气球
//
// 示例 2
//
//
//输入points = [[1,2],[3,4],[5,6],[7,8]]
//输出4
//
//
// 示例 3
//
//
//输入points = [[1,2],[2,3],[3,4],[4,5]]
//输出2
//
//
// 示例 4
//
//
//输入points = [[1,2]]
//输出1
//
//
// 示例 5
//
//
//输入points = [[2,3],[2,3]]
//输出1
//
//
//
//
// 提示:
//
//
// 1 <= points.length <= 104
// points[i].length == 2
// -231 <= xstart < xend <= 231 - 1
//
// Related Topics 贪心 数组 排序
// 👍 445 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.Comparator;
//452:用最少数量的箭引爆气球
class MinimumNumberOfArrowsToBurstBalloons{
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumNumberOfArrowsToBurstBalloons().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) {
return 0;
}
Arrays.sort(points, Comparator.comparingInt(point -> point[1]));
int num = points[0][1];
int count = 1;
for (int[] point: points) {
if (point[0] > num) {
num = point[1];
count++;
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}