akashtadwai

job-scheduling

Aug 7th, 2021 (edited)
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. class Solution:
  2.     def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
  3.         N = len(jobDifficulty)
  4.         if d > N:
  5.             #not enough tasks to split into each day
  6.             return -1
  7.         dp = [[float("inf")]*d for _ in range(N)]
  8.         #fill out first column based on max(jobDiffculty[0:i])
  9.         dp[0][0] = jobDifficulty[0]
  10.         for i in range(1,N):
  11.             dp[i][0] = max(dp[i-1][0],jobDifficulty[i])
  12.  
  13.         #fill out dp
  14.         for i in range(1,N):
  15.             for j in range(1,min(i+1,d)):
  16.                 #i+1 is the crossed out boxes since there's not enough tasks to break up into multiple days
  17.                 #d is the right end of the dp grid
  18.                 for k in range(i):
  19.                     #this is where we take terms that are calculated already on k,j-1 tile
  20.                     dp[i][j] = min(dp[i][j], dp[k][j-1] + max(jobDifficulty[k+1:i+1]))
  21.                     #Note that min() is tring to get the lowest difficulty among the k possible break-ups of #tasks=i,#days=j
  22.         return dp[-1][-1]
Add Comment
Please, Sign In to add comment