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 want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).
You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.
You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].
Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.
Example 1:
Input: jobDifficulty = [6,5,4,3,2,1], d = 2 Output: 7 Explanation: First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7
Example 2:
Input: jobDifficulty = [9,9,9], d = 4 Output: -1 Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
Example 3:
Input: jobDifficulty = [1,1,1], d = 3 Output: 3 Explanation: The schedule is one job per day. total difficulty will be 3.
Constraints:
1 <= jobDifficulty.length <= 3000 <= jobDifficulty[i] <= 10001 <= d <= 10Problem summary: You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i). You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day. You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i]. Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[6,5,4,3,2,1] 2
[9,9,9] 4
[1,1,1] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1335: Minimum Difficulty of a Job Schedule
class Solution {
public int minDifficulty(int[] jobDifficulty, int d) {
final int inf = 1 << 30;
int n = jobDifficulty.length;
int[][] f = new int[n + 1][d + 1];
for (var g : f) {
Arrays.fill(g, inf);
}
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= Math.min(d, i); ++j) {
int mx = 0;
for (int k = i; k > 0; --k) {
mx = Math.max(mx, jobDifficulty[k - 1]);
f[i][j] = Math.min(f[i][j], f[k - 1][j - 1] + mx);
}
}
}
return f[n][d] >= inf ? -1 : f[n][d];
}
}
// Accepted solution for LeetCode #1335: Minimum Difficulty of a Job Schedule
func minDifficulty(jobDifficulty []int, d int) int {
n := len(jobDifficulty)
f := make([][]int, n+1)
const inf = 1 << 30
for i := range f {
f[i] = make([]int, d+1)
for j := range f[i] {
f[i][j] = inf
}
}
f[0][0] = 0
for i := 1; i <= n; i++ {
for j := 1; j <= min(d, i); j++ {
mx := 0
for k := i; k > 0; k-- {
mx = max(mx, jobDifficulty[k-1])
f[i][j] = min(f[i][j], f[k-1][j-1]+mx)
}
}
}
if f[n][d] == inf {
return -1
}
return f[n][d]
}
# Accepted solution for LeetCode #1335: Minimum Difficulty of a Job Schedule
class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
f = [[inf] * (d + 1) for _ in range(n + 1)]
f[0][0] = 0
for i in range(1, n + 1):
for j in range(1, min(d + 1, i + 1)):
mx = 0
for k in range(i, 0, -1):
mx = max(mx, jobDifficulty[k - 1])
f[i][j] = min(f[i][j], f[k - 1][j - 1] + mx)
return -1 if f[n][d] >= inf else f[n][d]
// Accepted solution for LeetCode #1335: Minimum Difficulty of a Job Schedule
struct Solution;
use std::collections::HashMap;
impl Solution {
fn min_difficulty(job_difficulty: Vec<i32>, d: i32) -> i32 {
let n = job_difficulty.len();
let d = d as usize;
if d > n {
return -1;
}
let mut memo: HashMap<(usize, usize), i32> = HashMap::new();
Self::dp(0, d, &mut memo, &job_difficulty, n)
}
fn dp(
start: usize,
d: usize,
memo: &mut HashMap<(usize, usize), i32>,
jobs: &[i32],
n: usize,
) -> i32 {
if let Some(&res) = memo.get(&(start, d)) {
return res;
}
let res = if d == 1 {
*jobs[start..n].iter().max().unwrap()
} else {
if start + d == n {
jobs[start..start + d].iter().sum()
} else {
let mut min = std::i32::MAX;
let mut max = 0;
for i in start..=(n - d) {
max = max.max(jobs[i]);
min = min.min(max + Self::dp(i + 1, d - 1, memo, jobs, n));
}
min
}
};
memo.insert((start, d), res);
res
}
}
#[test]
fn test() {
let job_difficulty = vec![6, 5, 4, 3, 2, 1];
let d = 2;
let res = 7;
assert_eq!(Solution::min_difficulty(job_difficulty, d), res);
let job_difficulty = vec![9, 9, 9];
let d = 4;
let res = -1;
assert_eq!(Solution::min_difficulty(job_difficulty, d), res);
let job_difficulty = vec![1, 1, 1];
let d = 3;
let res = 3;
assert_eq!(Solution::min_difficulty(job_difficulty, d), res);
let job_difficulty = vec![7, 1, 7, 1, 7, 1];
let d = 3;
let res = 15;
assert_eq!(Solution::min_difficulty(job_difficulty, d), res);
let job_difficulty = vec![11, 111, 22, 222, 33, 333, 44, 444];
let d = 6;
let res = 843;
assert_eq!(Solution::min_difficulty(job_difficulty, d), res);
}
// Accepted solution for LeetCode #1335: Minimum Difficulty of a Job Schedule
function minDifficulty(jobDifficulty: number[], d: number): number {
const n = jobDifficulty.length;
const inf = 1 << 30;
const f: number[][] = new Array(n + 1).fill(0).map(() => new Array(d + 1).fill(inf));
f[0][0] = 0;
for (let i = 1; i <= n; ++i) {
for (let j = 1; j <= Math.min(d, i); ++j) {
let mx = 0;
for (let k = i; k > 0; --k) {
mx = Math.max(mx, jobDifficulty[k - 1]);
f[i][j] = Math.min(f[i][j], f[k - 1][j - 1] + mx);
}
}
}
return f[n][d] < inf ? f[n][d] : -1;
}
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.