中山大学算法课程题目详解(第八周)
来源:互联网 发布:如何给网络加密 编辑:程序博客网 时间:2024/06/07 01:48
问题描述:
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.
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).
解决思路:
具体实现代码如下:
int findMinArrowShots(vector<pair<int, int>>& points) {if (points.empty()) return 0;sort(points.begin(), points.end());int res = 1, end = points[0].second;for (int i = 1; i < points.size(); i++) {if (points[i].first <= end) {end = min(end, points[i].second);}else {res++;end = points[i].second;}}return res;}
- 中山大学算法课程题目详解(第八周)
- 中山大学算法课程题目详解(第二周)
- 中山大学算法课程题目详解(第三周)
- 中山大学算法课程题目详解(第五周)
- 中山大学算法课程题目详解(第六周)
- 中山大学算法课程题目详解(第七周)
- 中山大学算法课程题目详解(第九周)
- 中山大学算法课程题目详解(第十周)
- 中山大学算法课程题目详解(第十二周)
- 中山大学算法课程题目详解(第一周)
- 中山大学算法课程题目详解(第四周)
- 中山大学算法课程题目详解(第十一周)
- Leetcode算法课程第八周(补)
- [第八届蓝桥杯]-题目详解
- 算法分析与设计课程作业第八周#1
- 中山大学软件学院的课程
- 中山大学软件学院的课程
- coursera机器学习课程第八周——课程笔记(一)
- js及jQuery移除html元素所有的class
- SpringMVC 4.1 整合fastjson
- 三极管的工作原理-1
- uisearchbar的实现,自定义searcher
- 关于协方差矩阵的理解
- 中山大学算法课程题目详解(第八周)
- hdu 5091 Beam Cannon(线段树) 矩形覆盖最多点
- tomcat8压测监控调优
- Gradle高级构建工具简述及流程
- %d 跟%i 的区别以及scanf和sscanf的用法
- Android项目中自定义顶部标题栏
- 接口回调
- 目前比较牛X流行的前端框架
- CV还要更热闹!旷视刚宣布4.6亿美元融资,商汤:新一轮年内完成