LeetCode 452. Minimum Number of Arrows to Burst Balloons
来源:互联网 发布:java构造器有什么用 编辑:程序博客网 时间:2024/06/09 16:41
452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
Example:
Input:
[[10,16], [2,8], [1,6], [7,12]]Output:
2Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
题目内容:
题目给出一个数组,其中数组的每个元素代表一个气球的起始位置
解题思路:
这一道题可以用贪心算法(Greedy Algorithms)来解决。首先对所有气球按照
代码:
//// main.cpp// 452. Minimum Number of Arrows to Burst Balloons//// Created by mingjc on 2017/4/16.// Copyright © 2017年 mingjc. All rights reserved.//#include <iostream>#include <vector>#include <algorithm>using namespace std;class Solution {public: int findMinArrowShots(vector<pair<int, int>>& points) { if (points.empty()) return 0; sort(points.begin(), points.end(), compare); int currentArrowPos = points[0].second; int arrowCnt = 1; for (int i = 0; i < points.size(); i++) { if (points[i].first <= currentArrowPos) { continue; } arrowCnt++; currentArrowPos = points[i].second; } return arrowCnt; } static bool compare(pair<int, int> p0, pair<int, int> p1) { return p0.second < p1.second; }};int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; return 0;}
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons【M】
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- Leetcode 452. Minimum Number of Arrows to Burst Balloons[medium]
- [leetcode] 452. Minimum Number of Arrows to Burst Balloons
- Leetcode-452. Minimum Number of Arrows to Burst Balloons
- 【LeetCode】 452. Minimum Number of Arrows to Burst Balloons
- [Leetcode] 452. Minimum Number of Arrows to Burst Balloons
- [leetcode]452. Minimum Number of Arrows to Burst Balloons
- [LeetCode]452. Minimum Number of Arrows to Burst Balloons
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- 452. Minimum Number of Arrows to Burst Balloons| Leetcode Greedy
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- leetcode 452. Minimum Number of Arrows to Burst Balloons
- Leetcode 452. Minimum Number of Arrows to Burst Balloons
- JDK、JRE、JVM三者间的关系
- JavaScript高级程序设计-学习笔记1 (第一章--第四章)
- 1905-06-22 00:00:00.000
- poj 3617 Best Cow Line(简单贪心)
- hadoop学习(9)—— 启动方式
- LeetCode 452. Minimum Number of Arrows to Burst Balloons
- 传统定时器技术-笔记整理2
- 关于CT中pitch 的解释 (What is pitch?)
- 03、React系列之--ES6的新特性
- 平衡小车学习记录
- 04、React系列之--开发工具Atom配置
- linux下的文件链接
- Big Data课程总结 ( 1 )
- 05、React系列之--永不过时的Hello world