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