leetcode 452用少量的箭射爆气球
2024-09-28 08:11:14
类似于区间调度问题,使用贪心算法:首先对所有气球按照起始坐标大小排序,然后每次总是优先选择起始坐标小的气球中的右边坐标,然后再选择下一个;
排完序之后,下一个可能有如上图所示几种情况,
1) 当next.end<t时,此时一定有next.start>start且next.start<t, 应该令t=next.end这样可以节省一支箭;
2) 当next.end>t且next.start<end, 此时一定可以射爆next,参考虚线最顶端的气球;
3) 当next.start>t时,此时一定需要增加一支箭来射穿next, 且当前的箭不能再多射中气球;
class Solution {
public:
static bool cmp(pair<int,int>a, pair<int,int>b ){
return a.first<b.first;
}
int findMinArrowShots(vector<pair<int, int>>& points) {
sort(points.begin(),points.end(),cmp);
int len=points.size();
//空points边界判定
if(len==) return ; /***
1) 当next.end<t时,此时一定有next.start>start且next.start<t, 应该令t=next.end这样可以节省一支箭; 2) 当next.end>t且next.start<end, 此时一定可以射爆next,参考虚线最顶端的气球; 3) 当next.start>t时,此时一定需要增加一支箭来射穿next, 且当前的箭不能再多射中气球;***/
int t=points[].second;
int cnt=;
for(int i=;i<len;i++){
//1),2)可以认为下一个if不成立时选end最小值;
if(points[i].second<t){
t=points[i].second;
//cout<<"1: "<<t<<endl;
}
//符合3的情况,必须多一支箭
if(t<points[i].first){
cnt++;t=points[i].second;
//cout<<"2: "<<t<<endl;
}
}
return cnt;
}
};
最新文章
- vs2015 已经支持开发asp .net core 1.0 rc2 程序了
- maven出现 -Dmaven.multiModuleProjectDirectory system propery错误
- vios 多 vlan设置
- 让mysql支持emoji表情
- 《大象-Think In UML》读书笔记3
- 遇到一个奇葩的问题,could not load the assembly file XXX downloaded from the Web
- Lucene总体架构
- 卷积神经网络(CNN)
- 【bzoj3110】[Zjoi2013]K大数查询
- 用PYTHON硬写SOCKET
- ajax异步服务器获取时间
- Hadoop 使用FileSystem API 读取数据
- Redis详解(一)------ redis的简介与安装
- jsp内置对象-session对象
- [Go] golang类型断言
- 【原创】JAVA8之妙用Optional解决NPE问题
- HIkari线程池调优
- 网页提示504 gateway time-out是什么意思?如何解决?
- [zz]VC2005-应用程序正常初始化失败-0xc0150002
- 1. Spring boot 之热部署
热门文章
- Redis总结1
- 3、Eclipse 的SVN 插件
- 用ant打包
- deep_learning_Function_tensorflow_random_normal_initializer
- linux工具之pmap
- IO模型(epoll)--详解-02
- 心态炸了,我再写一个4.1.0版本的SND实例
- Prometheus+Granfana
- Spring JdbcTemplate + transactionTemplate 简单示例 (零配置)
- 阅读之spring+Dubbo