75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

1011. Capacity To Ship Packages Within D Days

Binary search on answer

Code

class Solution {
public:
    int shipWithinDays(vector<int>& weights, int days)
    {
        int left = 0, right = 0;
        for (int w : weights) {
            left = max(left, w);
            right += w;
        }
        while (left < right) {
            int mid = (left + right) / 2, need = 1, cur = 0;
            for (int w : weights) {
                if (cur + w > mid) {
                    need +++;
                    cur = 0;
                }
                cur += w;
            }
            if (need > days)
                left = mid + 1;
            else
                right = mid;
        }
        return left;
    }
};