LeetCode solutions

1825. Find Minimum Time to Finish All Jobs

My accepted C++ solution to LeetCode problem 1825, Find Minimum Time to Finish All Jobs, running in 0ms.

  • Difficulty: Hard
  • C++
  • Runtime 0ms
  • Memory 9.7MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 9.7MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    int minimumTimeRequired(vector<int>& jobs, int k) {
        sort(jobs.rbegin(), jobs.rend());
        int left = jobs[0], right = accumulate(jobs.begin(), jobs.end(), 0);
        while (left < right) {
            int mid = left + (right - left) / 2;
            vector<int> workers(k, 0);
            if (canFinish(jobs, workers, 0, mid)) right = mid;
            else left = mid + 1;
        }
        return left;
    }

    bool canFinish(vector<int>& jobs, vector<int>& workers, int idx, int limit) {
        if (idx == jobs.size()) return true;
        int cur = jobs[idx];
        for (int i = 0; i < workers.size(); i++) {
            if (workers[i] + cur <= limit) {
                workers[i] += cur;
                if (canFinish(jobs, workers, idx + 1, limit)) return true;
                workers[i] -= cur;
            }
            if (workers[i] == 0) break;
        }
        return false;
    }
};

Source