Untitled
unknown
java
3 years ago
790 B
4
Indexable
public static /** * @param n the number of packages * @param weights the weights of all packages 1 through n. Note that weights[0] should be ignored! * @param maxWeight the maximum weight a truck can carry * @return the minimal number of trucks required to ship the packages _in the given order_. */ int minAmountOfTrucks(int n, int[] weights, int maxWeight) { int trucks = 0; int currWeight = 0; for(int i = 1; i <= n; i++) { if(currWeight + weights[i] > maxWeight) { trucks++; currWeight = weights[i]; if(i == n && weights[i] <= maxWeight) { trucks++; } } else { currWeight += weights[i]; } } return trucks; } }
Editor is loading...