There are a row of n houses, 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 the same color.

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

example:

Given costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10

Note:
All costs are positive integers.

解题思路:

房子i的最小涂色开销是房子i-1的最小涂色开销,加上房子i本身的涂色开销。但是房子i的涂色方式需要根据房子i-1的涂色方式来确定,所以我们对房子i-1要记录涂三种颜色分别不同的开销,这样房子i在涂色的时候,我们就知道三种颜色各自的最小开销是多少了。我们在原数组上修改,可以做到不用空间。

State: dp[i][j] // three colors: j = 0 or 1 or 2,

Function:

dp[i][0] = dp[i][0] + min(dp[i - 1][1], dp[i -1][2])

dp[i][1] = dp[i][1] + min(dp[i - 1][0], dp[i - 1][2])

dp[i][2] = dp[i][2] + min(dp[i - 1][0], dp[i - 1][1])    

Initialize: dp = costs

Return: min(dp[n][0], dp[n][1], dp[n][2])

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

public class Solution {
public int minCost(int[][] costs) {
int len = costs.length;
if(costs != null && len == 0) return 0;
int[][] dp = costs;
for(int i = 1; i < len; i++){
dp[i][0] = costs[i][0] + Math.min(costs[i - 1][1], costs[i - 1][2]);
dp[i][1] = costs[i][1] + Math.min(costs[i - 1][0], costs[i - 1][2]);
dp[i][2] = costs[i][2] + Math.min(costs[i - 1][0], costs[i - 1][1]);
}
return Math.min(dp[len - 1][0], Math.min(dp[len - 1][1], dp[len - 1][2]));
} public static void main(String args[]) {
int[][] costs = new int[][]{{14,2,11},{11,14,5},{14,3,10}};
Solution sol = new Solution();
System.out.println(sol.minCost(costs));
}
}

   

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

class Solution {
public int minCost(int[][] costs) {
if(costs != null && costs.length == 0) return 0;
// 直接用原数组
for(int i = 1; i < costs.length; i++){
// 涂第一种颜色的话,上一个房子就不能涂第一种颜色,这样我们要在上一个房子的第二和第三个颜色的最小开销中找最小的那个加上
costs[i][0] = costs[i][0] + Math.min(costs[i - 1][1], costs[i - 1][2]);
// 涂第二或者第三种颜色同理
costs[i][1] = costs[i][1] + Math.min(costs[i - 1][0], costs[i - 1][2]);
costs[i][2] = costs[i][2] + Math.min(costs[i - 1][0], costs[i - 1][1]);
}
// 返回涂三种颜色中开销最小的那个
return Math.min(costs[costs.length - 1][0], Math.min(costs[costs.length - 1][1], costs[costs.length - 1][2]));
}
}

  

  

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

  1. [leetcode]256. Paint House粉刷房子(三色可选)

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

  2. [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 ...

  3. [LintCode] Paint House 粉刷房子

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

  4. [LeetCode#256] Paint House

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

  5. [LeetCode] 256. Paint House_Easy tag: Dynamic Programming

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

  6. [LeetCode] 276. Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

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

  8. [LeetCode] 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. [Swift]LeetCode256.粉刷房子 $ 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. 微信小程序~Flex布局

    有一点需要注意的是,你的小程序要求兼容到iOS8以下版本,需要开启样式自动补全.开启样式自动补全,在“设置”—“项目设置”—勾选“上传代码时样式自动补全”.

  2. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  3. vscode——常用插件记录

    前言 本人vscode中使用的插件列表,记录下. 列表 Auto Rename Tag 自动重命名成对的超文本标记语言/可扩展标记语言 background-cover 为vscode设置背景图片 C ...

  4. Jenkins拉取github库代码执行构建

    前言 上篇文章写了关于定时构建,以及构建后发送邮件的内容,但是构建时运行的代码是我们手动添加到Jenkins工作空间的.这篇文章我们说一说自动从GitHub远程库拉取代码,执行构建,废话不多说,开始! ...

  5. Java中多态

    多态:把子类看成是父类,把实现类看成是接口,这样类就具有多种形态,称为多态. 在多态中访问成员变量,访问的是父类中的成员变量. 在多态中访问成员方法,先访问的是子类,看看子类有没有覆盖重写要访问的父类 ...

  6. c++ sprintf() 用法

    1. char  boxData[100]; fi.mWidth = 1.0, fi.mCenter_x= 2.1, fi.mCenter_y=1.1; sprintf(boxData, " ...

  7. webuploader解决大文件断点续传

    文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...

  8. 2019.12.06 java基础代码

    操作系统中默认码表是:gbk      (一个中文字符占两个字节): utf-8(一个中文字符占三个字节): 数据库建库时的默认码表是:拉丁码表: (1)       public class 定义: ...

  9. C# VS 调试 动态加载的 DLL

    原文:https://www.cnblogs.com/DasonKwok/p/10510218.html 在这篇文章的底部,有提供示例的Demo,可以参考一下哦,拿来直接就可以运行. 说明: 编译类库 ...

  10. 金字塔原理(Pyramid Principle)

    什么是金字塔原理?简单来说,金字塔原理就是“中心论点---分论点---支撑论据”这样的一个结构. 图片摘自:http://www.woshipm.com/pmd/306704.html 人类通常习惯于 ...