[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 };
时间: 2024-10-20 08:53:46