452. Minimum Number of Arrows to Burst Balloons(greedy)
来源:互联网 发布:什么是php网站 编辑:程序博客网 时间:2024/06/16 15:54
1. Description
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:
2
Explanation:
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).
2. Problem Analysis
(待)
3. Possible solution
class Solution {public: static bool compare(const pair<int, int>& p1, const pair<int, int>& p2) { return p1.first == p2.first? p1.second < p2.second : p1.first < p2.first; } int findMinArrowShots(vector<pair<int, int> >& points) { if(points.size() == 0) return 0; sort(points.begin(), points.end(), compare); int count = 0; int size = points.size(); int j; for(int i = 0; i < size; ++i) { j = i+1; // 如果有多个区间存在交集 while( j < size && points[i].second >= points[j].first) { points[i].second = min(points[i].second, points[j].second); j++; } count++; i = j-1;//i即将从 j 开始 } return count; }};
- 452. Minimum Number of Arrows to Burst Balloons(greedy)
- 452. Minimum Number of Arrows to Burst Balloons| Leetcode Greedy
- 452. Minimum Number of Arrows to Burst Balloons(Medium)
- greedy——452. Minimum Number of Arrows to Burst Balloons[medium]
- Minimum Number of Arrows to Burst Balloons
- Minimum Number of Arrows to Burst Balloons
- 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】452. Minimum Number of Arrows to Burst Balloons【M】
- 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
- 452. Minimum Number of Arrows to Burst Balloons
- Leetcode 452. Minimum Number of Arrows to Burst Balloons[medium]
- 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
- RecyclerView源码分析
- AngularJS中$http服务
- C++模板(二)-类模板
- 【安全牛学习笔记】WPS及其他工具
- LeetCode编程练习
- 452. Minimum Number of Arrows to Burst Balloons(greedy)
- 51nod 1130 N的阶乘的长度 V2(斯特林近似)
- 怎么导入github上的maven项目到eclipse中
- gym101431D(传说中的构造(呸))
- cocos2d-x(lua)的安装和运行helloworld(一)
- LeetCode编程练习
- 什么是CDN?CDN有什么用处?
- error: conversion from 'std::_List_const_iterator<_Mylist>' to non-scalar type 'std::_List_iterator<
- 基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制(二)FreeRTOS v9.0.0 的移植