[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 ...
随机推荐
- redis.conf 配置信息:读取及修改命令
相关资源 网址 官方地址(网页中 Command + F,输入井号"#",方便查看没有注释的行) http://download.redis.io/redis-stable/red ...
- prettier-eslint 与 prettier-eslint-cli 区别
prettier-eslint 与 prettier-eslint-cli 区别: prettier-eslint 只能处理字符串 prettier-eslint-cli 能处理一个或多个文件 默认情 ...
- c#读sql server数据添加到MySQL数据库
using System;using System.Collections.Generic;using System.Text;using Console = System.Console;using ...
- linux下C语言多线程编程实例
用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...
- Android SurfaceView及TextureView对比
SurfaceView是什么? 它继承自类View,因此它本质上是一个View.但与普通View不同的是,它有自己的Surface.有自己的Surface,在WMS中有对应的WindowState,在 ...
- crossdomain.xml配置不当的利用和解决办法
00x1: 今天在无聊的日站中发现了一个flash小站,点进crossdomain.xml一看,震惊 本屌看到这个*就发觉事情不对 百度一下,这是一个老洞,配置不当能引起各种问题就算能远程加载恶意的s ...
- Vue--父子组件之间的传值
1.父传子 1.在父组件中给子组件绑定动态属性 <v-home :title="title"></v-home> 2.在子组件中用propos[]接收 ex ...
- k8s学习笔记之六:Pod控制器(kube-controller-manager)
第一章.什么是kube-controller-manager? Controller Manager 由 kube-controller-manager 和 cloud-controller-mana ...
- win10环境下Android studio安装教程----亲测可用
这段时间学习了一下Android的基本开发,发现Google已经停止了对eclipse的支持,并开发了自己的Android开发工具--Android Studio,于是想安装一下Android Stu ...
- python-web自动化环境安装
web自动化环境安装 1.安装selenium 命令行使用以下命令安装selenium:pip install -U selenium 2.安装chrome浏览器 3.chromedriver的下载 ...