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, costs0 is the cost of painting house 0 with color 0; costs1 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的拓展,这题的解法的思路还是用DP,那道题只让用红绿蓝三种颜色来粉刷房子,而这道题让我们用k种颜色,这道题不能用之前那题的Math.min方法了,会TLE。只要把最小和次小的都记录下来就行了,用preMin和PreSec来记录之前房子的最小和第二小的花费的颜色,如果当前房子颜色和min1相同,那么我们用min2对应的值计算,反之我们用min1对应的值,这种解法实际上也包含了求次小值的方法。

State: dp[i][j]

Function: dp[i][j] = costs[i][j] + preMin or costs[i][j] + preSec

Initialize: preMin = 0 , preSec = 0

Return: dp[n][preMin]

Java: Time: O(n), Space: O(1)

public class Solution {
public int minCostII(int[][] costs) {
if(costs != null && costs.length == 0) return 0;
int prevMin = 0, prevSec = 0, prevIdx = -1;
for(int i = 0; i < costs.length; i++){
int currMin = Integer.MAX_VALUE, currSec = Integer.MAX_VALUE, currIdx = -1;
for(int j = 0; j < costs[0].length; j++){
costs[i][j] = costs[i][j] + (prevIdx == j ? prevSec : prevMin);
// 找出最小和次小的,最小的要记录下标,方便下一轮判断
if(costs[i][j] < currMin){
currSec = currMin;
currMin = costs[i][j];
currIdx = j;
} else if (costs[i][j] < currSec){
currSec = costs[i][j];
}
}
prevMin = currMin;
prevSec = currSec;
prevIdx = currIdx;
}
return prevMin;
}
}

  

相似题目:

256. Paint House

[LeetCode] 265. Paint House II 粉刷房子的更多相关文章

  1. [leetcode]265. Paint House II粉刷房子(K色可选)

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  2. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  3. [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 paintin ...

  4. [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 o ...

  5. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  6. 265. Paint House II 房子涂色K种选择的版本

    [抄题]: There are a row of n houses, each house can be painted with one of the k colors. The cost of p ...

  7. 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 pai ...

  8. LC 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 paintin ...

  9. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

随机推荐

  1. 助教总结---继alpha版本1之后

    本周心得: 在项目的开发当中,学生难免会有懈怠的时候,作为助教更应该去督促和激励同学们,但本质上该对自己负责任的是同学们自己.同学们项目的第一版本已经出来了,这个过程他们自己知道付出了多少,相信他们体 ...

  2. LeetCode LCP 3 机器人大冒险

    题目解析: 对于本题主要的核心是对于一个指令字符串如“RURUU”,如果我们假设它的终点坐标为(8,8),其实只要统计指令字符串中的R的个数和U的个数(对于我给出的例子而言,num_R == 2,nu ...

  3. P2606 [ZJOI2010]排列计数

    P2606 [ZJOI2010]排列计数 因为每个结点至多有一个前驱,所以我们可以发现这是一个二叉树.现在我们要求的就是以1为根的二叉树中,有多少种情况,满足小根堆的性质. 设\(f(i)\)表示以\ ...

  4. 在命令行中执行kms命令激活Microsoft Office 2010

    激活office2010的命令是什么?激活office2010除了使用office2010激活工具之外,还可以使用kms命令来激活office2010,但是office2010激活命令还需考虑32位或 ...

  5. 鸡尾酒排序Cocktail Sort (双向冒泡排序)

    鸡尾酒排序 鸡尾酒排序思路,先从左边开始进行冒泡排序,第一趟冒泡排序完,最大值在的数组的最右端,然后进行第二趟排序,第二趟排序从右边开始排序,第二趟结束时,最小值在数组最左端,以此类推,每一趟排序完都 ...

  6. 解决PHP处理图片时内存占用过高问题

    用过GD库的同学可能都知道,使用imagecreatetruecolor()函数创建一个真彩色的画布是第一步.但是,如果画布的宽高超过平常的宽高,会带来极大的内存消耗.比如,一个9600×4800的画 ...

  7. Djiango-建立模型抽象基类

    创建一个抽象模型基类 ‘ 然后 ’base_model.py from django.db import models from datetime import date class BaseMode ...

  8. Mybatis框架-@Param注解

    回顾一下上一个小demo中存在的问题,是是根据用户的id修改用户的密码,我们只是修改了用户的密码,结果我们的在写接口方法的时候掺入的参数确实一个User对象,这样让别人看到我们的代码真的是很难读懂啊! ...

  9. C# 基础回顾: volatile 关键字

    有些人可能从来没看到过这个关键字,这也难怪,因为这个关键字并不常用.那这个关键字到底有什么用呢? 我在网上搜索这个关键字的时候,发现很多朋友都有一个错误的认识 ------ 认为这个关键字可以防止并发 ...

  10. 做勇敢女孩 https://www.bilibili.com/video/av14346123?from=search&seid=14078047355739050009

    So a few years ago, I did something really brave, or some would say really stupid. I ran for congres ...