码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】贪心

时间:2017-03-28 22:03:53      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:nim   return   ide   number   线段   使用   segment   zha   find   

[452] Minimum Number of Arrows to Burst Balloons[Medium]

给一堆线段,使用最少的arrow,穿过所有的线段。陈题,第一条线段的终点。

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).
技术分享
 1 // 陈题。 第一条线段的终点。
 2 //wyzhang
 3 class Solution {
 4 public:
 5     static bool cmp(pair<int, int>& a, pair<int, int>& b) {
 6         return a.second < b.second;
 7     }
 8 
 9     int findMinArrowShots(vector<pair<int, int>>& points) {
10         sort(points.begin(), points.end(), cmp);
11         vector<bool> valid_segments(points.size(), true);
12 
13         int ans = 0;
14         for (size_t i = 0; i < points.size(); ++i) {
15             if(!valid_segments[i]) { // balloon has been shot
16                 continue;
17             }
18             const int end = points[i].second;
19             for (size_t j = i + 1; j < points.size(); ++j) {
20                 if (!valid_segments[j]) {
21                     continue;
22                 }
23                 if (end >= points[j].first) {
24                     valid_segments[j] = false;
25                 }
26             }
27             ans++;
28         }
29         return ans;
30     }
31 };
View Code

 

【LeetCode】贪心

标签:nim   return   ide   number   线段   使用   segment   zha   find   

原文地址:http://www.cnblogs.com/zhangwanying/p/6636728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!