[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 ...
随机推荐
- np金融量化分析
在所有的np中都是已返回值的形式进行修改的,否则不会修改 只是显示内容 形状是三维数据 全0数组 reshape也可以将二维的变成一维的 下标和切片 一维的切片 二维切片 . 列表切片 给一个数组 ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- SQL SERVER 月、季、年统计与常用查询语句汇总
一.SQL SERVER 月.季.年统计查询 --本天 SELECT *FROM dbo.TableName WHERE DATEDIFF(DAY,TimeField,getdate())= 0; - ...
- Twitter的雪花算法(snowflake)自增ID
前言 这个问题源自于,我想找一个分布式下的ID生成器. 这个最简单的方案是,数据库自增ID.为啥不用咧?有这么几点原因,一是,会依赖于数据库的具体实现,比如,mysql有自增,oracle没有,得用序 ...
- [转]jvm调优-命令大全(jps jstat jmap jhat jstack jinfo)
运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...
- Oracle中存储图片的类型为BLOB类型,Java中如何将其读取并转为字符串?
一,读取图片转为String类型: 需要使用Sun公司提供的Base64工具 String str = ((Map) list1.get(0)).get("EINVOICEFILE" ...
- Pycharm 设置上下左右快捷键
Pycharm的版本 Note:英文版的Pycharm,使用中文版的对照即可. 1. 打开Pycharm软件→File→Settings 2.Keymap→Editor Actions→搜索(up)→ ...
- Vsftp安装及配置主动模式/被动模式
第一章.前言 FTP的主动模式(active mode)和被动模式(passive mode) 大多数的TCP服务是使用单个的连接,一般是客户向服务器的一个周知端口发起连接,然后使用这个连接进行通讯 ...
- kickstart之中rootpw密码生成方法
一.简介 linux kickstart文件里rootpw密码可以使用明文,也可以使用加密过的值,这里主要介绍下三种加密方法:md5.sha256.sha512 使用明文的方法 rootpw &quo ...
- Oracle 学习笔记(五)
--表空间,auto: 自动管理, manual: 手动管理 create tablespace tsp1 datafile 'D:\ORACLE\ORADATA\O10\tsp1.dbf' ...