2
public int shipWithinDays(int[] weights, int days) {5
// left is the biggest element in the array. It's set as the lower boundary.6
// right is the sum of the array, which is the upper limit.7
for (int weight : weights) {8
left = Math.max(weight, left);12
while (left <= right) {13
int mid = (left + right) / 2;14
// make sure mid is a possible value15
if (isPossible(weights, days, mid)) {25
public boolean isPossible(int[] weights, int days, int mid) {28
for (int i = 0; i < weights.length; i++) {29
totalWeight += weights[i];30
// increase totalDays if totalWeight is larger than mid31
if (totalWeight > mid) {33
totalWeight = weights[i];35
// the problem states all the packages have to ship within `days` days36
if (totalDays > days) {