1335. 工作计划的最低难度

解题思路

动态规划

代码

class Solution {
public:
    int minDifficulty(vector<int>& jobDifficulty, int d) {
        int n = jobDifficulty.size();
        if (n < d) {
            return -1;
        }

        vector<vector<int>> dp(d + 1, vector<int>(n, INT_MAX));
        int maxV = 0;
        for (int i = 0; i < n; ++i) {
            maxV = maxVx(maxV, jobDifficulty[i]);
            dp[0][i] = maxV;
        }        

        for (int i = 1; i < d; ++i) {
            for (int j = i; j < n; ++j) {
                maxV = 0;
                for (int k = j; k >= i; --k) {
                    maxV = maxVx(maxV, jobDifficulty[k]);
                    dp[i][j] = min(dp[i][j], maxV + dp[i - 1][k -1]);
                }
            }
        }

        return dp[d - 1][ n - 1];
    }
};

你可能感兴趣的:(#,《leetcode的刷题周记》,算法,c++,动态规划)