当前位置:网站首页>Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding

Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding

2022-06-25 04:16:00 HERODING23

 Insert picture description here

Their thinking :
A very basic dynamic programming problem , It can even be given directly in the question costs On the basis of , hold costs As an array of state transitions ,cost[i][0] Obviously, it was before i-1 Of 1 and 2 Color dependent ,cost[i][1] and cost[i][2] It's the same thing , So always take the minimum sum of each state , Return to , The code is as follows :

class Solution {
    
public:
    int minCost(vector<vector<int>>& costs) {
    
        int n = costs.size();
        for(int i = 1; i < n; i ++) {
    
            costs[i][0] += min(costs[i-1][1], costs[i-1][2]);
            costs[i][1] += min(costs[i-1][0], costs[i-1][2]);
            costs[i][2] += min(costs[i-1][0], costs[i-1][1]);
        }
        return min(costs[n-1][0], min(costs[n-1][1], costs[n-1][2]));
    }
};
原网站

版权声明
本文为[HERODING23]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250210308407.html