[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 ...
随机推荐
- 从客户端(content="xxxxx")中检测到有潜在危险的 Request.Form 值——较合理解决方案
1,修改配置文件: <httpRuntime requestValidationMode = "2.0" /> 以上修改是必须的,因为高版本的.netframework ...
- 【转】GnuPG使用介绍
一.什么是 GPG 要了解什么是 GPG,就要先了解 PGP. 1991 年,程序员 Phil Zimmermann 为了避开政府监视,开发了加密软件 PGP.这个软件非常好用,迅速流传开来,成了许多 ...
- 用python+openpyxl从表格中读取测试用例的多条数据,然后将执行结果写入表格中
# -*- coding: utf-8 -*- from selenium import webdriver from openpyxl import load_workbook class mylo ...
- POI不同浏览器导出名称处理
/** * * @Title: encodeFileName * @Description: 导出文件转换文件名称编码 * @param @param fileNames * @param @para ...
- VUE之路
最近研究了下Vue这个前端框架,不得不说这个前端框架很是厉害.不过对于习惯了jQuery的我来说,刚上手那会儿还是踩了很多的坑啊.那会儿觉得天啊,这个Vue框架特别的绕,并且也更复杂.不过待我写了几天 ...
- py3+requests+json+xlwt,爬取拉勾招聘信息
在拉勾搜索职位时,通过谷歌F12抓取请求信息 发现请求是一个post请求,参数为: 返回的是json数据 有了上面的基础,我们就可以构造请求了 然后对获取到的响应反序列化,这样就获取到了json格式的 ...
- pyinstaller打包多个py文件和去除cmd黑框
1.打包多个py文件并且去除cmd黑框 格式:pyinstaller.exe -F 路径\文件名.py空格路径\文件名.py空格--noconsole pyinstaller.exe -F ui.py ...
- lombok常用注解@Data@AllArgsConstructor@NoArgsConstructor@Builder@Accessors
原贴:https://blog.csdn.net/ChenXvYuan_001/article/details/84961992 https://blog.csdn.net/weixin_382293 ...
- BZOJ 3689: 异或之 可持久化trie+堆
和超级钢琴几乎是同一道题吧... code: #include <bits/stdc++.h> #define N 200006 #define ll long long #define ...
- WinDbg常用命令系列---.write_cmd_hist (写命令历史记录)
.write_cmd_hist 简介 .write_cmd_hist命令将调试器命令窗口的整个历史记录写入文件. 使用形式 .write_cmd_hist Filename 参数 Filename指定 ...