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. Kotlin属性引用进阶与构造方法引用

    继续还是探讨Kotlin反射相关的知识点,说实话这块不是太好理解,待在实际工作中去对它进行实践慢慢来加深印象. 属性引用进阶: 在Kotlin中的反射其实是跟Java的反射有对应关系的,具体相关的定义 ...

  2. C# 保存文件到数据库

    html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploa ...

  3. assert 断言

    输入 assert 1>2,'123' 输出结果 assert 1>2,'123' AssertionError: 123

  4. JQuery系列(4) - AJAX方法

    jQuery对象上面还定义了Ajax方法($.ajax()),用来处理Ajax操作.调用该方法后,浏览器就会向服务器发出一个HTTP请求. $.ajax方法 $.ajax()的用法主要有两种. $.a ...

  5. formData上传文件

    需要将选中的xml传到后台,通过xslt转换为html html: <form id="uploadForm" enctype="multipart/form-da ...

  6. Java 内部类和Lambda

    Java内部类 内部类又称为嵌套类,是在类中定义另外一个类.内部类可以处于方法内/外,内部类的成员变量/方法名可以和外部类的相同.内部类编译后会成为完全不同的两个类,分别为outer.class和ou ...

  7. go 学习 (二):基本语法

    一.数据类型 布尔型:布尔型的值只可以是常量 true 或者 false.eg:var bo bool = true.布尔型无法参与数值运算,也无法与其他类型进行转换 数字类型:整型 int .浮点型 ...

  8. RSDS pdb格式

    本描述了“RSDS”或“DS”类型的pdb(程序数据库)文件的格式,这些文件是由Miscrosoft的link.exe从版本7及更高版本发出的. 什么是PDB文件? 如果选择了/DEBUG选项或/DE ...

  9. NuxtJS如何利用axios异步请求

    第一种:使用nuxt 提供的 Axios插件 @nuxtjs/axios 1.安装:npm install @nuxtjs/axios -d 2.配置 nuxt.config.js exports d ...

  10. CSP内容安全策略总结及如何抵御 XSS 攻击

    跨域脚本攻击 XSS 是最常见.危害最大的网页安全漏洞.为了防止它们,要采取很多编程措施,非常麻烦.很多人提出,能不能根本上解决问题,浏览器自动禁止外部注入恶意脚本?这就是"网页安全政策&q ...