265. Paint House II

There are a row of n houses, each house can be painted with one of the k colors. 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.

The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Follow up:
Could you solve it in O(nk) runtime?

此题是paint house的升级,和paint house不同之处在于,每个房子粉刷的颜色不再是常数了,而是k,此题想了很久没有什么思路,便直接看大神的解题思路了。

首先定义两个最小值,一个是第一小的值,一个是第二小的值,代表遍历到每个房子各个颜色的成本最小值,和第二小的值。在粉刷下一个房子的时候,如果之前的房子所包含的颜色不在最小成本值里面,就把最小成本值加上去,如果在最小成本值里面,则把第二小的值加进去。代码如下:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 }

public class Solution {

public int minCostII(int[][] costs) {

if(costs==null||costs.length==0||costs[0].length==0) return 0;

int min1 = -1;

int min2 = -1;

int m = costs.length;

int k = costs[0].length;

for(int i=0;i<m;i++){

int last1= min1;

int last2 = min2;

min1 = -1;

min2 = -1;

for(int j=0;j<k;j++){

if(last1!=j){

costs[i][j]+=last1<0?0:costs[i-1][last1];

}else{

costs[i][j]+=last1<0?0:costs[i-1][last2];

}

if(min1<0||costs[i][j]<costs[i][min1]){

min2 = min1;

min1 = j;

}else if(min2<0||costs[i][j]<costs[i][min2]){

min2 = j;

}

}

}

return costs[m-1][min1];

}

}

时间: 2024-10-24 08:46:02

265. Paint House II的相关文章

[LeetCode#265] Paint House II

Problem: There are a row of n houses, each house can be painted with one of the k colors. 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. The co

*Paint House II

There are a row of n houses, each house can be painted with one of the k colors. 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. The cost of pai

Paint House II 解答

Question There are a row of n houses, each house can be painted with one of the k colors. 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. The co

[LintCode] Paint House II 粉刷房子之二

There are a row of n houses, each house can be painted with one of the k colors. 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. The cost of pai

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L

练几道,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca