You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a **limit** on the **number of boxes** and the **total weight** that it can carry.

You are given an array `boxes`

, where `boxes[i] = [ports`

, and three integers _{i}, weight_{i}]`portsCount`

, `maxBoxes`

, and `maxWeight`

.

`ports`

is the port where you need to deliver the_{i}`i`

box and^{th}`weights`

is the weight of the_{i}`i`

box.^{th}`portsCount`

is the number of ports.`maxBoxes`

and`maxWeight`

are the respective box and weight limits of the ship.

The boxes need to be delivered **in the order they are given**. The ship will follow these steps:

- The ship will take some number of boxes from the
`boxes`

queue, not violating the`maxBoxes`

and`maxWeight`

constraints. - For each loaded box
**in order**, the ship will make a**trip**to the port the box needs to be delivered to and deliver it. If the ship is already at the correct port, no**trip**is needed, and the box can immediately be delivered. - The ship then makes a return
**trip**to storage to take more boxes from the queue.

The ship must end at storage after all the boxes have been delivered.

Return *the minimum number of trips the ship needs to make to deliver all boxes to their respective ports.*

**Example 1:**

Input:boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3Output:4Explanation:The optimal strategy is as follows: - 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. So the total number of trips is 4. Note 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).

**Example 2:**

Input:boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6Output:6Explanation:The optimal strategy is as follows: - The ship takes the first box, goes to port 1, then returns to storage. 2 trips. - The ship takes the second, third and fourth boxes, goes to port 3, then returns to storage. 2 trips. - The ship takes the fifth box, goes to port 3, then returns to storage. 2 trips. So the total number of trips is 2 + 2 + 2 = 6.

**Example 3:**

Input:boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7Output:6Explanation:The optimal strategy is as follows: - The ship takes the first and second boxes, goes to port 1, then returns to storage. 2 trips. - The ship takes the third and fourth boxes, goes to port 2, then returns to storage. 2 trips. - The ship takes the fifth and sixth boxes, goes to port 3, then returns to storage. 2 trips. So the total number of trips is 2 + 2 + 2 = 6.

**Example 4:**

Input:boxes = [[2,4],[2,5],[3,1],[3,2],[3,7],[3,1],[4,4],[1,3],[5,2]], portsCount = 5, maxBoxes = 5, maxWeight = 7Output:14Explanation:The optimal strategy is as follows: - The ship takes the first box, goes to port 2, then storage. 2 trips. - The ship takes the second box, goes to port 2, then storage. 2 trips. - The ship takes the third and fourth boxes, goes to port 3, then storage. 2 trips. - The ship takes the fifth box, goes to port 3, then storage. 2 trips. - The ship takes the sixth and seventh boxes, goes to port 3, then port 4, then storage. 3 trips. - The ship takes the eighth and ninth boxes, goes to port 1, then port 5, then storage. 3 trips. So the total number of trips is 2 + 2 + 2 + 2 + 3 + 3 = 14.

**Constraints:**

`1 <= boxes.length <= 10`

^{5}`1 <= portsCount, maxBoxes, maxWeight <= 10`

^{5}`1 <= ports`

_{i}<= portsCount`1 <= weights`

_{i}<= maxWeight

**Solution: Sliding Window**

Time complexity: O(n)

Space complexity: O(n)

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Author: Huahua class Solution { public: int boxDelivering(vector<vector<int>>& boxes, int portsCount, int maxBoxes, int maxWeight) { const int n = boxes.size(); vector<int> dp(n + 1); // dp[i] := min trips to deliver boxes[0:i] for (int i = 0, j = 0, b = 0, w = 0, t = 1; j < n; ++j) { // Different ports. if (j == 0 || boxes[j][0] != boxes[j - 1][0]) ++t; // Load the box. w += boxes[j][1]; while (w > maxWeight // Too heavy. || (j - i + 1) > maxBoxes // Too many boxes. || (i < j && dp[i + 1] == dp[i])) { // Same cost more boxes. w -= boxes[i++][1]; // 'Unload' the box. if (boxes[i][0] != boxes[i - 1][0]) --t; // Different ports. } // Takes t trips to deliver boxes[i~j]. dp[j + 1] = dp[i] + t; } return dp[n]; } }; |

请尊重作者的劳动成果，转载请注明出处！花花保留对文章／视频的所有权利。

如果您喜欢这篇文章／视频，欢迎您捐赠花花。

If you like my articles / videos, donations are welcome.

## Be First to Comment