{"id":7796,"date":"2020-12-13T01:23:23","date_gmt":"2020-12-13T09:23:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7796"},"modified":"2020-12-13T10:54:19","modified_gmt":"2020-12-13T18:54:19","slug":"leetcode-1687-delivering-boxes-from-storage-to-ports","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1687-delivering-boxes-from-storage-to-ports\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1687. Delivering Boxes from Storage to Ports"},"content":{"rendered":"\n<p>You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a&nbsp;<strong>limit<\/strong>&nbsp;on the&nbsp;<strong>number of boxes<\/strong>&nbsp;and the&nbsp;<strong>total weight<\/strong>&nbsp;that it can carry.<\/p>\n\n\n\n<p>You are given an array&nbsp;<code>boxes<\/code>, where&nbsp;<code>boxes[i] = [ports<sub>\u200b\u200bi<\/sub>\u200b, weight<sub>i<\/sub>]<\/code>, and three integers&nbsp;<code>portsCount<\/code>,&nbsp;<code>maxBoxes<\/code>, and&nbsp;<code>maxWeight<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>ports<sub>\u200b\u200bi<\/sub><\/code>&nbsp;is the port where you need to deliver the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;box and&nbsp;<code>weights<sub>i<\/sub><\/code>&nbsp;is the weight of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;box.<\/li><li><code>portsCount<\/code>&nbsp;is the number of ports.<\/li><li><code>maxBoxes<\/code>&nbsp;and&nbsp;<code>maxWeight<\/code>&nbsp;are the respective box and weight limits of the ship.<\/li><\/ul>\n\n\n\n<p>The boxes need to be delivered&nbsp;<strong>in the order they are given<\/strong>. The ship will follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The ship will take some number of boxes from the&nbsp;<code>boxes<\/code>&nbsp;queue, not violating the&nbsp;<code>maxBoxes<\/code>&nbsp;and&nbsp;<code>maxWeight<\/code>&nbsp;constraints.<\/li><li>For each loaded box&nbsp;<strong>in order<\/strong>, the ship will make a&nbsp;<strong>trip<\/strong>&nbsp;to the port the box needs to be delivered to and deliver it. If the ship is already at the correct port, no&nbsp;<strong>trip<\/strong>&nbsp;is needed, and the box can immediately be delivered.<\/li><li>The ship then makes a return&nbsp;<strong>trip<\/strong>&nbsp;to storage to take more boxes from the queue.<\/li><\/ul>\n\n\n\n<p>The ship must end at storage after all the boxes have been delivered.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of&nbsp;<strong>trips<\/strong>&nbsp;the ship needs to make to deliver all boxes to their respective ports.<\/em><\/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> boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The optimal strategy is as follows: \n- The ship takes all the boxes in the queue, goes to port 1, then port 2, then port 1 again, then returns to storage. 4 trips.\nSo the total number of trips is 4.\nNote that the first and third boxes cannot be delivered together because the boxes need to be delivered in order (i.e. the second box needs to be delivered at port 2 before the third box).\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> boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The optimal strategy is as follows: \n- The ship takes the first box, goes to port 1, then returns to storage. 2 trips.\n- The ship takes the second, third and fourth boxes, goes to port 3, then returns to storage. 2 trips.\n- The ship takes the fifth box, goes to port 3, then returns to storage. 2 trips.\nSo the total number of trips is 2 + 2 + 2 = 6.\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> boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The optimal strategy is as follows:\n- The ship takes the first and second boxes, goes to port 1, then returns to storage. 2 trips.\n- The ship takes the third and fourth boxes, goes to port 2, then returns to storage. 2 trips.\n- The ship takes the fifth and sixth boxes, goes to port 3, then returns to storage. 2 trips.\nSo the total number of trips is 2 + 2 + 2 = 6.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> boxes = [[2,4],[2,5],[3,1],[3,2],[3,7],[3,1],[4,4],[1,3],[5,2]], portsCount = 5, maxBoxes = 5, maxWeight = 7\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong> The optimal strategy is as follows:\n- The ship takes the first box, goes to port 2, then storage. 2 trips.\n- The ship takes the second box, goes to port 2, then storage. 2 trips.\n- The ship takes the third and fourth boxes, goes to port 3, then storage. 2 trips.\n- The ship takes the fifth box, goes to port 3, then storage. 2 trips.\n- The ship takes the sixth and seventh boxes, goes to port 3, then port 4, then storage. 3 trips. \n- The ship takes the eighth and ninth boxes, goes to port 1, then port 5, then storage. 3 trips.\nSo the total number of trips is 2 + 2 + 2 + 2 + 3 + 3 = 14.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= boxes.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= portsCount, maxBoxes, maxWeight &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= ports<sub>\u200b\u200bi<\/sub>&nbsp;&lt;= portsCount<\/code><\/li><li><code>1 &lt;= weights<sub>i<\/sub>&nbsp;&lt;= maxWeight<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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\nclass Solution {\npublic:\n  int boxDelivering(vector<vector<int>>& boxes, int portsCount, int maxBoxes, int maxWeight) {\n    const int n = boxes.size();\n    vector<int> dp(n + 1); \/\/ dp[i] := min trips to deliver boxes[0:i]\n    for (int i = 0, j = 0, b = 0, w = 0, t = 1; j < n; ++j) {\n      \/\/ Different ports.\n      if (j == 0 || boxes[j][0] != boxes[j - 1][0]) ++t;      \n      \/\/ Load the box.\n      w += boxes[j][1];\n      while (w > maxWeight  \/\/ Too heavy.\n             || (j - i + 1) > maxBoxes \/\/ Too many boxes.\n             || (i < j &#038;&#038; dp[i + 1] == dp[i])) { \/\/ Same cost more boxes.\n        w -= boxes[i++][1]; \/\/ 'Unload' the box.\n        if (boxes[i][0] != boxes[i - 1][0]) --t; \/\/ Different ports.\n      }\n      \/\/ Takes t trips to deliver boxes[i~j].\n      dp[j + 1] = dp[i] + t;\n    }\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a&nbsp;limit&nbsp;on the&nbsp;number of boxes&nbsp;and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[18,217,215],"class_list":["post-7796","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-dp","tag-hard","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7796","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=7796"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7796\/revisions"}],"predecessor-version":[{"id":7798,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7796\/revisions\/7798"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}