1335. Minimum Difficulty of a Job Schedule

Description

You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the i-th 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 in that day.

Given an array of integers jobDifficulty and an integer d. The difficulty of the i-th job is jobDifficulty[i].

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

Example

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 

Note

1 <= jobDifficulty.length <= 300
0 <= jobDifficulty[i] <= 1000
1 <= d <= 10

分析

虽然归类为 hard 基本的 dp 题目,但是实际上还不如 meidum 难度的题目

code

class Solution(object):
    def minDifficulty(self, N, K):
        """
        :type jobDifficulty: List[int]
        :type d: int
        :rtype: int
        """
        dp = [[float('inf') for _ in range(len(N))] for _ in range(len(N))]
        dp2 = [[float('inf') for _ in range(len(N))] for _ in range(K)]
        for i in range(len(N)):
            dp[i][i] = N[i]
        if K > len(N):
            return -1
        for i in range(len(N)):
            for j in range(i+1, len(N)):
                dp[i][j] = max(N[j], dp[i][j-1])

        for i in range(len(N)):
            dp2[0][i] = dp[0][i]

        for k in range(1, K):
            dp2[k][k] = sum(N[:k+1])
            for i in range(k, len(N)):
                for j in range(k-1, i+1):
                    dp2[k][i] = min(dp2[k-1][j-1]+dp[j][i], dp2[k][i])

        return dp2[K-1][-1]

总结

Runtime: 648 ms, faster than 85.71% of Python online submissions for Minimum Difficulty of a Job Schedule.
Memory Usage: 16 MB, less than 8.00% of Python online submissions for Minimum Difficulty of a Job Schedule.
  • 十几分钟就搞定了~,之前一直在思考有没有性能更好的写法。结果没有。也是 testcase 不太强,不然肯定 tle
posted @ 2020-07-08 19:41  tmortred  阅读(373)  评论(0编辑  收藏  举报