[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 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 粉刷房子的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- java代码转python代码
(1)安装工具(windows 环境下面) 先下载antlr: http://www.antlr3.org/download/antlr-3.1.3.tar.gz 链接:http://pan.baid ...
- 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题
如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...
- kubectl kubernetes cheatsheet
from : https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 PDF Link: cheatsheet-kubernetes-A4 ...
- Django 中使用redis
Django使用redis 方式一,使用Django-redis模块 #安装: pip3 install django-redis CACHES = { "default": ...
- 洛谷 [USACO05DEC] 布局 题解
今天学了差分约束系统, 这是一道板子题. 核心:a[v]>a[u]+d 相当于从u到v连一条长度为d的有向边.由于要判断有环,所以要从0点先跑一遍spfa因为1点不一定能到所有的点. #incl ...
- B/S结构与C/S结构测试区别
B/S结构与C/S结构 B/S结构是浏览器/服务器结构,应用软件的业务逻辑完全在服务器端实现,客户端只需要通过浏览器完成浏览.查询.输入等简单操作. C/S结构是客户端/浏览器结构,客户端具有一定的数 ...
- 内核用户模式调试支持(Dbgk)
简介 将详细分析Windows调试的内核模式接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全 ...
- .NET体系结构
主要内容包括: C#与.NET的关系.公共语言运行库.中间语言.程序集..NET Framework类.名称空间.内层管理... C#与.NET的关系 C#是门高级编程语言,.NET(Framewor ...
- svn项目迁移至gitlab
关于svn项目迁移有人可能会说,新建一个git项目,把原来的代码直接扔进去提交不完了吗.恩,是的,没错.但是为了保留之前的历史提交记录,还是得做下面的步骤 首先确保本地正常安装配置好git,具体步骤不 ...
- TCP网络程序设计
tcp_server.c #include<stdio.h>#include<sys/socket.h>#include<string.h>#include< ...