{"id":2926,"date":"2018-06-23T07:56:20","date_gmt":"2018-06-23T14:56:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2926"},"modified":"2018-06-24T13:40:33","modified_gmt":"2018-06-24T20:40:33","slug":"leetcode-452-minimum-number-of-arrows-to-burst-balloons","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-452-minimum-number-of-arrows-to-burst-balloons\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 452. Minimum Number of Arrows to Burst Balloons"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 452. Minimum Number of Arrows to Burst Balloons - \u5237\u9898\u627e\u5de5\u4f5c EP197\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/DguJN47_mSg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>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&#8217;s horizontal, y-coordinates don&#8217;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 10<sup>4<\/sup>\u00a0balloons.<\/p>\n<p>An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with x<sub>start<\/sub>\u00a0and x<sub>end<\/sub>bursts by an arrow shot at x if x<sub>start<\/sub>\u00a0\u2264 x \u2264 x<sub>end<\/sub>. 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.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b>\r\n[[10,16], [2,8], [1,6], [7,12]]\r\n\r\n<b>Output:<\/b>\r\n2\r\n\r\n<b>Explanation:<\/b>\r\nOne 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).\r\n<\/pre>\n<h1><strong>Solution: Sweep Line<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 82 ms\r\nclass Solution {\r\npublic:\r\n  int findMinArrowShots(vector&lt;pair&lt;int, int&gt;&gt;&amp; points) {\r\n    if (points.empty()) return 0;\r\n    sort(points.begin(), points.end(), \r\n         [&amp;](const pair&lt;int,int&gt;&amp; a, const pair&lt;int,int&gt;&amp; b){\r\n           return a.second &lt; b.second;\r\n         });\r\n    int right = points.front().second;\r\n    int ans = 1;\r\n    for (const auto&amp; point : points) {\r\n      if (point.first &gt; right) {\r\n        right = point.second;\r\n        ++ans;\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-56-merge-intervals\/\">\u82b1\u82b1\u9171 LeetCode 56. Merge Intervals<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-57-insert-interval\/\">\u82b1\u82b1\u9171 LeetCode 57. Insert Interval<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-759-employee-free-time\/\">\u82b1\u82b1\u9171 LeetCode 759. Employee Free Time<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-729-my-calendar-i\/\">\u82b1\u82b1\u9171 LeetCode 729. My Calendar I<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-731-my-calendar-ii\/\">\u82b1\u82b1\u9171 LeetCode 731. My Calendar II &amp;#8211; \u82b1\u82b1\u9171<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/732-my-calendar-iii\/\">\u82b1\u82b1\u9171 LeetCode 732. My Calendar III<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127,51],"tags":[284,88,104],"class_list":["post-2926","post","type-post","status-publish","format-standard","hentry","category-geometry","category-greedy","tag-geometry","tag-greedy","tag-sweep-line","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2926"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2926\/revisions"}],"predecessor-version":[{"id":2929,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2926\/revisions\/2929"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}