Delivering Boxes from Storage to Ports LeetCode solution in JavaScript

1
/**
2
* @param {number[][]} boxes
3
* @param {number} portsCount
4
* @param {number} maxBoxes
5
* @param {number} maxWeight
6
* @return {number}
7
*/
8
var boxDelivering = function (boxes, portsCount, maxBoxes, maxWeight) {
9
const trips = Array(boxes.length + 1).fill(0);
10
let left = 0;
11
let diff = 0;
12

13
for (let right = 0; right < boxes.length; right++) {
14
maxBoxes--;
15
maxWeight -= boxes[right][1];
16
if (right > 0 && boxes[right][0] !== boxes[right - 1][0]) diff++;
17

18
while (
19
maxBoxes < 0 ||
20
maxWeight < 0 ||
21
(left < right && trips[left + 1] === trips[left])
22
) {
23
maxBoxes++;
24
maxWeight += boxes[left++][1];
25
if (boxes[left][0] !== boxes[left - 1][0]) diff--;
26
}
27

28
trips[right + 1] = diff + 2 + trips[left];
29
}
30

31
return trips[boxes.length];
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0