-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetMaxUnits.js
More file actions
33 lines (31 loc) · 949 Bytes
/
getMaxUnits.js
File metadata and controls
33 lines (31 loc) · 949 Bytes
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
33
function getMaxUnits(boxes, unitsPerBox, truckSize) {
let maxUnits = 0;
let calcBoxRestantes = truckSize;
while (unitsPerBox.length !== 0) {
const cantMaxCaja = Math.max(...unitsPerBox);
const indexCaja = unitsPerBox.findIndex((cant) => cant == cantMaxCaja);
const unitMaxBox = boxes[indexCaja];
if (unitMaxBox == 1) {
maxUnits += cantMaxCaja;
calcBoxRestantes -= unitMaxBox;
unitsPerBox.splice(indexCaja, 1);
boxes.splice(indexCaja, 1);
} else {
let vueltasCaja = unitMaxBox;
while (vueltasCaja > 0) {
vueltasCaja -= 1;
maxUnits += cantMaxCaja;
calcBoxRestantes -= 1;
}
unitsPerBox.splice(indexCaja, 1);
boxes.splice(indexCaja, 1);
}
if (unitsPerBox.length == 0) {
break;
}
}
return maxUnits;
}
console.log(
getMaxUnits([4, 6, 5, 2, 7], [46335, 90039, 24796, 87808, 17739], 610563)
);