Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the ith job.
There are k workers that you can assign jobs to. Each job should be assigned to exactly one worker. The working time of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the maximum working time of any worker is minimized.
Return the minimum possible maximum working time of any assignment.
Example 1:
Input: jobs = [3,2,3], k = 3 Output: 3 Explanation: By assigning each person one job, the maximum time is 3.
Example 2:
Input: jobs = [1,2,4,7,8], k = 2 Output: 11 Explanation: Assign the jobs the following way: Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11) Worker 2: 4, 7 (working time = 4 + 7 = 11) The maximum working time is 11.
Constraints:
1 <= k <= jobs.length <= 121 <= jobs[i] <= 107Problem summary: You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the ith job. There are k workers that you can assign jobs to. Each job should be assigned to exactly one worker. The working time of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the maximum working time of any worker is minimized. Return the minimum possible maximum working time of any assignment.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Backtracking · Bit Manipulation
[3,2,3] 3
[1,2,4,7,8] 2
minimum-number-of-work-sessions-to-finish-the-tasks)find-minimum-time-to-finish-all-jobs-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1723: Find Minimum Time to Finish All Jobs
class Solution {
private int[] cnt;
private int ans;
private int[] jobs;
private int k;
public int minimumTimeRequired(int[] jobs, int k) {
this.k = k;
Arrays.sort(jobs);
for (int i = 0, j = jobs.length - 1; i < j; ++i, --j) {
int t = jobs[i];
jobs[i] = jobs[j];
jobs[j] = t;
}
this.jobs = jobs;
cnt = new int[k];
ans = 0x3f3f3f3f;
dfs(0);
return ans;
}
private void dfs(int i) {
if (i == jobs.length) {
int mx = 0;
for (int v : cnt) {
mx = Math.max(mx, v);
}
ans = Math.min(ans, mx);
return;
}
for (int j = 0; j < k; ++j) {
if (cnt[j] + jobs[i] >= ans) {
continue;
}
cnt[j] += jobs[i];
dfs(i + 1);
cnt[j] -= jobs[i];
if (cnt[j] == 0) {
break;
}
}
}
}
// Accepted solution for LeetCode #1723: Find Minimum Time to Finish All Jobs
func minimumTimeRequired(jobs []int, k int) int {
cnt := make([]int, k)
ans := 0x3f3f3f3f
sort.Slice(jobs, func(i, j int) bool {
return jobs[i] > jobs[j]
})
var dfs func(int)
dfs = func(i int) {
if i == len(jobs) {
mx := slices.Max(cnt)
ans = min(ans, mx)
return
}
for j := 0; j < k; j++ {
if cnt[j]+jobs[i] >= ans {
continue
}
cnt[j] += jobs[i]
dfs(i + 1)
cnt[j] -= jobs[i]
if cnt[j] == 0 {
break
}
}
}
dfs(0)
return ans
}
# Accepted solution for LeetCode #1723: Find Minimum Time to Finish All Jobs
class Solution:
def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
def dfs(i):
nonlocal ans
if i == len(jobs):
ans = min(ans, max(cnt))
return
for j in range(k):
if cnt[j] + jobs[i] >= ans:
continue
cnt[j] += jobs[i]
dfs(i + 1)
cnt[j] -= jobs[i]
if cnt[j] == 0:
break
cnt = [0] * k
jobs.sort(reverse=True)
ans = inf
dfs(0)
return ans
// Accepted solution for LeetCode #1723: Find Minimum Time to Finish All Jobs
/**
* [1723] Find Minimum Time to Finish All Jobs
*
* You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the i^th job.
* There are k workers that you can assign jobs to. Each job should be assigned to exactly one worker. The working time of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the maximum working time of any worker is minimized.
* Return the minimum possible maximum working time of any assignment.
*
* Example 1:
*
* Input: jobs = [3,2,3], k = 3
* Output: 3
* Explanation: By assigning each person one job, the maximum time is 3.
*
* Example 2:
*
* Input: jobs = [1,2,4,7,8], k = 2
* Output: 11
* Explanation: Assign the jobs the following way:
* Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)
* Worker 2: 4, 7 (working time = 4 + 7 = 11)
* The maximum working time is 11.
*
* Constraints:
*
* 1 <= k <= jobs.length <= 12
* 1 <= jobs[i] <= 10^7
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs/
// discuss: https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
// Credit: https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs/solutions/3199728/just-a-runnable-solution/
pub fn minimum_time_required(jobs: Vec<i32>, k: i32) -> i32 {
let mut jobs = jobs;
jobs.sort_unstable();
let mut left = jobs.iter().sum::<i32>() / k;
let mut right = jobs.iter().sum::<i32>();
while left < right {
let mid = (left + right) / 2;
if Self::check(&jobs, k, mid) {
right = mid;
} else {
left = mid + 1;
}
}
left
}
fn check(jobs: &[i32], k: i32, limit: i32) -> bool {
let mut workloads = vec![0; k as usize];
Self::dfs_helper(jobs, &mut workloads, 0, limit)
}
fn dfs_helper(jobs: &[i32], workloads: &mut [i32], i: usize, limit: i32) -> bool {
if i >= jobs.len() {
return true;
}
let cur = jobs[i];
for j in 0..workloads.len() {
if workloads[j] + cur <= limit {
workloads[j] += cur;
if Self::dfs_helper(jobs, workloads, i + 1, limit) {
return true;
}
workloads[j] -= cur;
}
if workloads[j] == 0 || workloads[j] + cur == limit {
break;
}
}
false
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1723_example_1() {
let jobs = vec![3, 2, 3];
let k = 3;
let result = 3;
assert_eq!(Solution::minimum_time_required(jobs, k), result);
}
#[test]
fn test_1723_example_2() {
let jobs = vec![1, 2, 4, 7, 8];
let k = 2;
let result = 11;
assert_eq!(Solution::minimum_time_required(jobs, k), result);
}
}
// Accepted solution for LeetCode #1723: Find Minimum Time to Finish All Jobs
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1723: Find Minimum Time to Finish All Jobs
// class Solution {
// private int[] cnt;
// private int ans;
// private int[] jobs;
// private int k;
//
// public int minimumTimeRequired(int[] jobs, int k) {
// this.k = k;
// Arrays.sort(jobs);
// for (int i = 0, j = jobs.length - 1; i < j; ++i, --j) {
// int t = jobs[i];
// jobs[i] = jobs[j];
// jobs[j] = t;
// }
// this.jobs = jobs;
// cnt = new int[k];
// ans = 0x3f3f3f3f;
// dfs(0);
// return ans;
// }
//
// private void dfs(int i) {
// if (i == jobs.length) {
// int mx = 0;
// for (int v : cnt) {
// mx = Math.max(mx, v);
// }
// ans = Math.min(ans, mx);
// return;
// }
// for (int j = 0; j < k; ++j) {
// if (cnt[j] + jobs[i] >= ans) {
// continue;
// }
// cnt[j] += jobs[i];
// dfs(i + 1);
// cnt[j] -= jobs[i];
// if (cnt[j] == 0) {
// break;
// }
// }
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.