{"id":4981,"date":"2019-03-16T23:45:00","date_gmt":"2019-03-17T06:45:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4981"},"modified":"2020-01-31T23:18:28","modified_gmt":"2020-02-01T07:18:28","slug":"leetcode-1011-capacity-to-ship-packages-within-d-days","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1011-capacity-to-ship-packages-within-d-days\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1011. Capacity To Ship Packages Within D Days"},"content":{"rendered":"\n<p>A conveyor belt has packages that must be shipped from one port to another within&nbsp;<code>D<\/code>&nbsp;days.<\/p>\n\n\n\n<p>The&nbsp;<code>i<\/code>-th package on the conveyor belt has a weight of&nbsp;<code>weights[i]<\/code>.&nbsp; Each day, we load the ship with packages on the conveyor belt (in the order given by&nbsp;<code>weights<\/code>). We may not load more weight than the maximum weight capacity of the ship.<\/p>\n\n\n\n<p>Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within&nbsp;<code>D<\/code>&nbsp;days.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>weights = [1,2,3,4,5,6,7,8,9,10], D = 5\n<strong>Output: <\/strong>15\n<strong>Explanation: <\/strong>\nA ship capacity of 15 is the minimum to ship all the packages in 5 days like this:\n1st day: 1, 2, 3, 4, 5\n2nd day: 6, 7\n3rd day: 8\n4th day: 9\n5th day: 10\n\nNote that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>weights = [3,2,2,4,1,4], D = 3\n<strong>Output: <\/strong>6\n<strong>Explanation: <\/strong>\nA ship capacity of 6 is the minimum to ship all the packages in 3 days like this:\n1st day: 3, 2\n2nd day: 2, 4\n3rd day: 1, 4\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>weights = [1,2,3,1,1], D = 4\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong>\n1st day: 1\n2nd day: 2\n3rd day: 3\n4th day: 1, 1\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= D &lt;= weights.length &lt;= 50000<\/code><\/li><li><code>1 &lt;= weights[i] &lt;= 500<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Find the smallest capacity such that can finish in D days.<\/p>\n\n\n\n<p>Time complexity: O(n * log(sum(weights))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 52 ms, 11.9 MB\nclass Solution {\npublic:\n  int shipWithinDays(vector<int>& weights, int D) {\n    int l = *max_element(begin(weights), end(weights));\n    int r = accumulate(begin(weights), end(weights), 0) + 1;\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      int d = 1;\n      int t = 0;\n      for (int w : weights)\n        if ((t += w) > m) {\n          t = w;\n          ++d;\n        } \n      d > D ? l = m + 1 : r = m;        \n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A conveyor belt has packages that must be shipped from one port to another within&nbsp;D&nbsp;days. The&nbsp;i-th package on the conveyor belt has a weight of&nbsp;weights[i].&nbsp;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177,179],"class_list":["post-4981","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4981","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=4981"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4981\/revisions"}],"predecessor-version":[{"id":6218,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4981\/revisions\/6218"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}