Skip to content

Commit 9b5f3e3

Browse files
authored
Create Minimum Difficulty of a Job Schedule.py
1 parent f041fc9 commit 9b5f3e3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#1335. Minimum Difficulty of a Job Schedule
2+
class Solution:
3+
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
4+
n = len(jobDifficulty)
5+
if d > n:
6+
return -1
7+
8+
# dp[i][k] := min difficulty to schedule the first i jobs in k days
9+
dp = [[math.inf] * (d + 1) for _ in range(n + 1)]
10+
dp[0][0] = 0
11+
12+
for i in range(1, n + 1):
13+
for k in range(1, d + 1):
14+
maxDifficulty = 0 # Max(job[j + 1..i])
15+
for j in range(i - 1, k - 2, -1): # 1-based
16+
# 0-based
17+
maxDifficulty = max(maxDifficulty, jobDifficulty[j])
18+
dp[i][k] = min(dp[i][k], dp[j][k - 1] + maxDifficulty)
19+
20+
return dp[n][d]

0 commit comments

Comments
 (0)