类似于区间调度问题,使用贪心算法:首先对所有气球按照起始坐标大小排序,然后每次总是优先选择起始坐标小的气球中的右边坐标,然后再选择下一个;

排完序之后,下一个可能有如上图所示几种情况,

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

最新文章

  1. vs2015 已经支持开发asp .net core 1.0 rc2 程序了
  2. maven出现 -Dmaven.multiModuleProjectDirectory system propery错误
  3. vios 多 vlan设置
  4. 让mysql支持emoji表情
  5. 《大象-Think In UML》读书笔记3
  6. 遇到一个奇葩的问题,could not load the assembly file XXX downloaded from the Web
  7. Lucene总体架构
  8. 卷积神经网络(CNN)
  9. 【bzoj3110】[Zjoi2013]K大数查询
  10. 用PYTHON硬写SOCKET
  11. ajax异步服务器获取时间
  12. Hadoop 使用FileSystem API 读取数据
  13. Redis详解(一)------ redis的简介与安装
  14. jsp内置对象-session对象
  15. [Go] golang类型断言
  16. 【原创】JAVA8之妙用Optional解决NPE问题
  17. HIkari线程池调优
  18. 网页提示504 gateway time-out是什么意思?如何解决?
  19. [zz]VC2005-应用程序正常初始化失败-0xc0150002
  20. 1. Spring boot 之热部署

热门文章

  1. Redis总结1
  2. 3、Eclipse 的SVN 插件
  3. 用ant打包
  4. deep_learning_Function_tensorflow_random_normal_initializer
  5. linux工具之pmap
  6. IO模型(epoll)--详解-02
  7. 心态炸了,我再写一个4.1.0版本的SND实例
  8. Prometheus+Granfana
  9. Spring JdbcTemplate + transactionTemplate 简单示例 (零配置)
  10. 阅读之spring+Dubbo