[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.
Note:
All costs are positive integers.
Example:
Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
Minimum cost: 2 + 5 + 3 = 10.
题意:
一排有n套房子,每个房子可以选一种颜色(红色、蓝色或者绿色)来粉刷。由于每个房子涂某种颜色的花费不同,求相邻房子不同色的粉刷最小花费。
Solution1: DP
scan一遍n套房子:
costs[i][0] = costs[i][0] + min(costs[i-1][1], costs[i-1][2])
当下选0号色的花费(和) = 当下选0号色的花费(单价) + 前套房子选1号色或者选2号色中较小花费


code
class Solution {
public int minCost(int[][] costs) {
if(costs == null || costs.length == 0 || costs[0].length < 3) return 0;
for(int i = 1; i < costs.length; i++ ){ // 照顾[i-1]所以 i 从 1 开始, 则 i - 1 从 0 开始。若从0开始,i 就只能取到costs.length-1
costs[i][0] += Math.min(costs[i-1][1], costs[i-1][2]);
costs[i][1] += Math.min(costs[i-1][0], costs[i-1][2]);
costs[i][2] += Math.min(costs[i-1][1], costs[i-1][0]);
}
int n = costs.length-1;
return Math.min(Math.min(costs[n][0], costs[n][1]), costs[n][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]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 ...
- [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 ...
- 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 ...
- [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#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] 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 ...
随机推荐
- 解决 windows下安装Anaconda后python pip不可用的情况
在windows系统下通过安装Anaconda的方式安装的python使用中发现不能再通过pip安装python包.只能通过conda install packname 的方法,导致很多conda不支 ...
- Mycat 镜像-创建 Docker 镜像
将 Mycat-server 创建到镜像,使其能够进行容器化部署,我们需要创建 Dockerfile 并在文件中安装其依赖项,使用 centos 做为 base 镜像,并安装 jdk 依赖即可,因此创 ...
- Delphi2009之TImage
TPngImage原来是SourceFroge上的一个开源项目,现在突然消失了,为什么呢?Nick 在他的博客上写到:TPNGImage被CodeGear/Embarcadero收购了,现在直接就是D ...
- Class 泛型
Java Class 泛型的例子说明: http://blog.chinaunix.net/uid-1911213-id-3085866.html http://blog.163.com/sir_87 ...
- [UE4]Expandable Area可展开的区域
一.Expandable Area控件分成头部和主体部分,头部和主体都是一个Named Slot,可以放置任何控件进去,点击头部会因此主体部分 1.如果勾选Size To Content,则收起的时候 ...
- 安装ORACLE高可用RAC集群11g校验集群安装的可行性输出信息
安装ORACLE高可用RAC集群11g校验集群安装的可行性输出信息 作者:Eric 微信:loveoracle11g [grid@node1 grid]$ ./runcluvfy.sh stage - ...
- 转:通过ASP.Net页面获取域用户名(当前登陆的用户)
通过ASP.Net页面获取域用户名(当前登陆的用户) 原文地址: https://www.cnblogs.com/fast-michael/archive/2011/03/14/2057954.htm ...
- Shiro+CAS
参考链接: CAS实现单点登录SSO执行原理探究:http://blog.csdn.net/javaloveiphone/article/details/52439613 单点登录CAS技术概述:ht ...
- 边缘触发(Edge Trigger)和条件触发(Level Trigger)
int select(int n, fd_set *rd_fds, fd_set *wr_fds, fd_set *ex_fds, struct timeval *timeout); sele ...
- eclipse中svn切换用户
如果永久保存svn用户账号及密码,再更换svn用户时需要先删除C:\Users\Administrator\AppData\Roaming下的Subversion文件,然后在刷新eclipse中的sv ...