-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1687.cpp
More file actions
32 lines (30 loc) · 1.03 KB
/
1687.cpp
File metadata and controls
32 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
int boxDelivering(vector<vector<int>>& boxes, int portsCount, int maxBoxes, int maxWeight) {
int n = boxes.size();
vector<int> dp(n + 1, INT_MAX / 2);
dp[0] = 0;
int right = 0;
int lastRight = 0;
int need = 0;
for (int left = 0; left < n; ++left) {
while (right < n && maxBoxes > 0 && boxes[right][1] <= maxWeight) {
maxBoxes--;
maxWeight -= boxes[right][1];
if (right == 0 || boxes[right][0] != boxes[right - 1][0]) {
need++;
lastRight = right;
}
right++;
}
dp[right] = min(dp[right], dp[left] + need + 1);
dp[lastRight] = min(dp[lastRight], dp[left] + need);
maxBoxes++;
maxWeight += boxes[left][1];
if (left == n - 1 || boxes[left][0] != boxes[left + 1][0]) {
need--;
}
}
return dp[n];
}
};