Minimum Difficulty of a Job Schedule - Problem

You're a project manager who needs to schedule a list of jobs over d days. The jobs have a strict dependency order - you must complete job i before you can start job i+1. Each job has a difficulty rating, and you must complete at least one job each day.

The difficulty of a day is determined by the most challenging job you complete that day. Your goal is to find a schedule that minimizes the total difficulty across all days.

Input: An integer array jobDifficulty where jobDifficulty[i] represents the difficulty of the i-th job, and an integer d representing the number of days available.

Output: Return the minimum total difficulty of the job schedule. If it's impossible to schedule all jobs in d days (i.e., you have more days than jobs), return -1.

Example: Jobs [6,5,4,3,2,1] over 2 days could be split as Day 1: [6,5,4] (difficulty = 6), Day 2: [3,2,1] (difficulty = 3), Total = 9.

Input & Output

example_1.py โ€” Basic Example
$ Input: jobDifficulty = [6,5,4,3,2,1], d = 2
โ€บ Output: 7
๐Ÿ’ก Note: One optimal schedule: Day 1 = [6], Day 2 = [5,4,3,2,1]. Day 1 difficulty = 6, Day 2 difficulty = max(5,4,3,2,1) = 5. Total = 6 + 5 = 11. But better schedule: Day 1 = [6,5,4,3,2], Day 2 = [1]. Day 1 difficulty = 6, Day 2 difficulty = 1. Total = 7.
example_2.py โ€” Single Day
$ Input: jobDifficulty = [9,9,9], d = 4
โ€บ Output: -1
๐Ÿ’ก Note: We have 3 jobs but need to schedule over 4 days. Since we must do at least one job per day, this is impossible.
example_3.py โ€” Equal Days and Jobs
$ Input: jobDifficulty = [1,1,1], d = 3
โ€บ Output: 3
๐Ÿ’ก Note: Each day we do exactly one job. Day 1: [1] (difficulty=1), Day 2: [1] (difficulty=1), Day 3: [1] (difficulty=1). Total = 1+1+1 = 3.

Constraints

  • 1 โ‰ค jobDifficulty.length โ‰ค 300
  • 0 โ‰ค jobDifficulty[i] โ‰ค 1000
  • 1 โ‰ค d โ‰ค 10
  • The jobs must be completed in the given order
  • At least one job must be completed each day

Visualization

Tap to expand
Job Scheduling OptimizationJobs: [6, 5, 4, 3, 2, 1] over 2 daysAttempt 1:6Day 1: max=654321Day 2: max=5Total: 6+5=11Attempt 2:65Day 1: max=64321Day 2: max=4Total: 6+4=10Optimal:65432Day 1: max=61Day 2: max=1Total: 6+1=7 โœ“Dynamic Programming explores all possibilities efficiently using memoization
Understanding the Visualization
1
Problem Setup
We have jobs [6,5,4,3,2,1] to schedule over 2 days. Each day's difficulty = max job that day.
2
Try Different Splits
Day 1: [6] + Day 2: [5,4,3,2,1] gives difficulty 6+5=11. Day 1: [6,5] + Day 2: [4,3,2,1] gives 6+4=10.
3
Find Optimal
Day 1: [6,5,4,3,2] + Day 2: [1] gives difficulty 6+1=7. This is optimal!
4
Dynamic Programming
Use memoization to avoid recalculating the same subproblems for different starting points and remaining days.
Key Takeaway
๐ŸŽฏ Key Insight: This problem has optimal substructure - the best way to schedule remaining jobs depends on previous optimal decisions. DP with memoization avoids recalculating the same subproblems, achieving O(nยฒd) time complexity instead of exponential.
Asked in
Google 45 Amazon 38 Meta 22 Microsoft 18
89.2K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen