leetcode 452. Minimum Number of Arrows to Burst Balloons
来源:互联网 发布:centos7 软件管理器 编辑:程序博客网 时间:2024/06/03 16:41
题目452. Minimum Number of Arrows to Burst Balloons
等级: medium
标签:贪心
思路
对pair,以第一个元素为第一排序准则,第二个元素为次要准则排序。
然后从小到大扫描,寻找连续的有交叠部分的pair,然后销毁,计数加一,继续此操作至尾部,计数结果即为答案
实现
# include <iostream># include <Vector># include <algorithm>using namespace std;bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first || (a.first == b.first && a.second < b.second);}class Solution {public: int findMinArrowShots(vector<pair<int, int> >& points) { int size = points.size(); if(size <= 1) return size; sort(points.begin(), points.end(), cmp); int index = 0, border = 0, ret = 0; border = points[index].second; while(1) { ret++; while(index < size && points[index].first <= border) { if(points[index].second < border) border = points[index].second; index++; }; if(index >= size) break; // update the border border = points[index].second; } return ret; }};
阅读全文
0 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
- Maven学习笔记
- JS封装类或对象的最佳方案
- linux 工作服务器环境切换
- 前端常用开发工具
- (洛谷 1108)低价购买
- leetcode 452. Minimum Number of Arrows to Burst Balloons
- 开发(仿微信)Android 日常总结(二)
- 【Python】自动生成命令行工具
- 包的初始化
- Text-mining and analytics学习笔记——第二周
- MyEclipse 使用javadoc导出项目的API文档
- 小鑫の日常系列故事(二)——石头剪子布
- 如何解决用 jedis 连接在虚拟机上连接redis 超时问题。
- 数据结构实验之排序五:归并求逆序数