代码(Go) const MAX = 0x3f3f3f3f func minCost(houses []int, cost [][]int, m int, n int, target int) int { // dp[i][j][k] 表示前 i 个房子中,共有 j 个街区, // 且第 i 个房子的颜色为 k 时的最小花费 dp := make([][][]int, m) for i := range dp { dp[i] =...
[LeetCode] Paint House Problem Description: There are a row ofnhouses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have...
house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10 LeetCode上的原题,请参见我之前的博客Paint House。 解法一: classSolution {public:/** * @param costs n x 3 cost matrix * @return an integer, the minimum cost to paint all houses*/intminCost(vector<vector<int...
There are a row of houses, each house can be painted with three colors red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with mi...
256. 265. Paint House https://leetcode.com/problems/paint-house/description/ image.png 因为有3种颜色,所以在建立DP的时候,需要维护3个状态,就是最后一个以什么颜色的HOSUE,最少要花多少。 dp[i][0] = min(dp[i-1 ][1],dp[i-1 ][2])+cost[i][0]转移方程就出来了。
paint i 和 ii一样,只是限定了k=3,所以可以写的简单一些。 paint ii https://leetcode.com/problems/paint-house-ii/ 未压缩空间: classSolution{public:intminCostII(vector<vector<int>>&costs){intn=costs.size();if(n==0)return0;intm=costs[0].size();vector<vector<int>>dp(n,vector<int>(...
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue. Minimum cost: 2 + 5 + 3 = 10. 这道题说有n个房子,每个房子可以用红绿蓝三种颜色刷,每个房子的用每种颜色刷的花费都不同,限制条件是相邻的房子不能用相同的颜色来刷,现在让求刷完所有的房子的最低花费是...
[LeetCode] 256. Paint House There is a row ofnhouses, where each house can be painted one of three colors: red, blue, or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color...
Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5. Follow up: Could you solve it in O(nk) runtime? 答案 直接改编Paint House题的答案 O(nk^2) classSolution{publicintminCostII(int[][]costs){if(costs.length==0)return0;intn=costs.length,k=cost...
LeetCode "Paint House" A typical DP class Solution { public: int minCost(vector<vector<int>>& costs) { size_t len = costs.size(); if(len == 0) return 0; vector<vector<int>> dp(len, vector<int>(3, 0)); dp[0] = costs[0]; for(int i = 1; i < len; i ++) { for(...