1011 (546B)
1 class Solution { 2 public: 3 int shipWithinDays(vector<int>& weights, int days) { 4 int left = 0, right = 25000000; 5 for (int w: weights) left = max(left, w); 6 while (left < right) { 7 int mid = (left + right) / 2, need = 1, cur = 0; 8 for (int i = 0; i < weights.size() && need <= days; cur += weights[i++]) 9 if (cur + weights[i] > mid) 10 cur = 0, need++; 11 if (need > days) left = mid + 1; 12 else right = mid; 13 } 14 return left; 15 } 16 };