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. 【python】requests 异常处理

    以下是request.exceptions下的各种异常错误: RequestException: HTTPError(RequestException) UnrewindableBodyError(R ...

  2. EasyUI之dataGrid的行内编辑

    $(function () { var datagrid; //定义全局变量datagrid var editRow = undefined; //定义全局变量:当前编辑的行 datagrid = T ...

  3. Linux shell脚本基础学习详细介绍(完整版)一

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...

  4. HBase学习笔记之HBase原理和Shell使用

    HBase学习指南之HBase原理和Shell使用 参考资料: 1.https://www.cnblogs.com/nexiyi/p/hbase_shell.html,hbase shell

  5. vue cli4.0 快速搭建项目详解

    搭建项目之前,请确认好你自己已经安装过node, npm, vue cli.没安装的可以参考下面的链接安装. 如何安装node? 安装好node默认已经安装好npm了,所以不用单独安装了. 如何安装v ...

  6. if语句的嵌套:从键盘输入3个实数,利用条件表达式求其最大者。

    #include<stdio.h>void main(){ float a,b,c,max; scanf("%f%f%f",&a,&b,&c); ...

  7. LeetCode 1059. All Paths from Source Lead to Destination

    原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/ 题目: Given the edges ...

  8. Oracle iops测试

    DECLARE  lat  INTEGER;  iops INTEGER;  mbps INTEGER;BEGIN  DBMS_RESOURCE_MANAGER.CALIBRATE_IO(4, 10, ...

  9. OKR的两个基本原则

    <启示录>作者,前易贝高级副总裁,硅谷产品集团创始人马蒂·卡根在<OKR工作法>的序言中提到了目标管理法的两个原则: 不要告诉下属具体怎么做,要告诉他们你要什么,他们就会给你满 ...

  10. velero 备份、迁移 kubernetes 应用以及持久化数据卷

    velero 是heptio 团队开源的kubernetes 应用以及持久化数据卷备份以及迁移的解决方案,以前的名字为ark 包含以下特性: 备份集群以及恢复 copy 当前集群的资源到其他集群 复制 ...