[Java][BinarySearch][LeetCode] Capacity To Ship Packages Within D Days #1011

SP Hou
1 min readJan 20, 2022

A conveyor belt has packages that must be shipped from one port to another within days days.

The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.

Example 1:

Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5
Output: 15

If dayCount>days, it’s meant, one day of capacity could raise. So, I shift left side to mid+1 to improve the capacity. On the contrary, it’s meant, one day of capacity need to revise down. I shift right side to mid to revise down the capacity. Because least have a solution, so count day from 1, and min capacity is from array max. It’s meant even one day delivery one package. we can delivery all package done at n day. As least have a solution too, I don’t need to check left value.

如果true,表示大於days,表示可以提升capacity的容量,
把mid往右邊移動就可以提升capacity,所以要把left往右邊移動。因為最少有一個解,所以從 1 開始計算天數,最小容量來自array最大值。 這意味著即使是一天交付一包。 我們可以在 n 天交付所有包裹。與至少也有一個解一樣,我不需要檢查左值。

--

--