力扣 1335. 工作计划的最低难度

我想到了区间的选择可能影响取值,然后满脑子递归递不出来,一看题解,dp。这属实不应该了,虽然以前dp其实也不会写,但还没到压根看不出来的程度。好歹题解还能看懂,入门级别的dp。

class Solution {
public:
    int minDifficulty(vector<int>& jobDifficulty, int d) {
	
	    int i, j, k, maxx;
	    int n = jobDifficulty.size();
	    if(n < d){
	    	return -1;
	    }
	    vector<vector<int> > dp(d + 1, vector<int>(n, 99999));
	    maxx = 0;
        for(i = 0; i < n; i ++){
            maxx = max(maxx, jobDifficulty[i]);
            dp[0][i] = maxx;
        }
	    for(i = 1; i < d; i ++){
	    	maxx = max(maxx, jobDifficulty[i]);
	    	for(j = i; j < n ;j ++){
	    		maxx = 0;
	    		for(k = j; k >= i ;k --){
	    			maxx = max(maxx, jobDifficulty[k]);
	    			dp[i][j] = min(dp[i][j], maxx + dp[i - 1][k - 1]);
	    		}
	    	}
	    }
	    return dp[d - 1][n - 1];
    }
};

你可能感兴趣的:(LeetCode,leetcode,算法,c++)