{"id":3435,"date":"2018-08-04T20:10:14","date_gmt":"2018-08-05T03:10:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3435"},"modified":"2018-08-04T20:16:15","modified_gmt":"2018-08-05T03:16:15","slug":"leetcode-885-boats-to-save-people","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-885-boats-to-save-people\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 885. Boats to Save People"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>The\u00a0<code>i<\/code>-th person has weight\u00a0<code>people[i]<\/code>, and each boat can carry a maximum weight of\u00a0<code>limit<\/code>.<\/p>\n<p>Each boat carries at most 2 people at the same time, provided the sum of the\u00a0weight of those people is at most\u00a0<code>limit<\/code>.<\/p>\n<p>Return the minimum number of boats to carry every given person.\u00a0 (It is guaranteed each person can be carried by a boat.)<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>people = <span id=\"example-input-1-1\">[1,2]<\/span>, limit = <span id=\"example-input-1-2\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<strong>Explanation: <\/strong>1 boat (1, 2)\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>people = <span id=\"example-input-2-1\">[3,2,2,1]<\/span>, limit = <span id=\"example-input-2-2\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">3<\/span>\r\n<strong>Explanation<\/strong>: 3 boats (1, 2), (2) and (3)\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>people = <span id=\"example-input-3-1\">[3,5,3,4]<\/span>, limit = <span id=\"example-input-3-2\">5<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">4<\/span>\r\n<strong>Explanation<\/strong>: 4 boats (3), (3), (4), (5)<\/pre>\n<p><strong>Note<\/strong>:<\/p>\n<ul>\n<li><code>1 &lt;=\u00a0people.length &lt;= 50000<\/code><\/li>\n<li><code>1 &lt;= people[i] &lt;=\u00a0limit &lt;= 30000<\/code><\/li>\n<\/ul>\n<h1><strong>Solution: Greedy + Two Pointers<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>Put one heaviest guy and put the lightest guy if not full.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 80 ms\r\nclass Solution {\r\npublic:\r\n  int numRescueBoats(vector&lt;int&gt;&amp; people, int limit) {\r\n    sort(people.rbegin(), people.rend());\r\n    int i = 0;\r\n    int j = people.size() - 1;\r\n    int ans = 0;\r\n    while (i &lt;= j) {\r\n      ++ans;\r\n      if (i == j) break;\r\n      if (people[i++] + people[j] &lt;= limit) --j;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem The\u00a0i-th person has weight\u00a0people[i], and each boat can carry a maximum weight of\u00a0limit. Each boat carries at most 2 people at the same time,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,176],"tags":[88,177,175],"class_list":["post-3435","post","type-post","status-publish","format-standard","hentry","category-greedy","category-two-pointers","tag-greedy","tag-medium","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3435","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=3435"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3435\/revisions"}],"predecessor-version":[{"id":3438,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3435\/revisions\/3438"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}