当前位置:网站首页>LeetCode.每日一题 剑指 Offer II 091. 粉刷房子 (DP问题)
LeetCode.每日一题 剑指 Offer II 091. 粉刷房子 (DP问题)
2022-07-01 10:42:00 【向光.】
剑指 Offer II 091. 粉刷房子
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的正整数矩阵 costs 来表示的。
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。
请计算出粉刷完所有房子最少的花费成本。
示例 1:
输入: costs = [[17,2,17],[16,16,5],[14,3,19]]
输出: 10
解释: 将 0 号房子粉刷成蓝色,1 号房子粉刷成绿色,2 号房子粉刷成蓝色。
最少花费: 2 + 5 + 3 = 10。
示例 2:
输入: costs = [[7,6,2]]
输出: 2
提示:
costs.length == n
costs[i].length == 3
1 <= n <= 100
1 <= costs[i][j] <= 20
Solution:
- 这里是显然的动态规划问题,我们要求的是粉刷完所有房子的最少花费成本,并且我们又知道,你给第i个房子刷漆时只跟它相邻的前一个房子的颜色有关,因此可以看出状态的转移在于相邻两个房子的颜色。
- 因此我们不妨设
dp[i][j]
表示粉刷前i个房子,且第i个房子刷j颜色所花费的最小成本,因此我们只需得到dp[n-1][0],dp[n-1][1],dp[n-1][2]
中的最小值即可,即得到粉刷0~n-1个房子,且第最后一个房子刷红色,蓝色,绿色所花费的最小成本,对其取min。
那么显然,我们根据相邻房子不能刷相同颜色的约定,可以通过循环遍历房子,对每次的刷漆状态进行转移即可,每次均取最小花费。
Code:
class Solution {
public int minCost(int[][] costs) {
int len = costs.length;
int[][] dp = new int[len][3];
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for(int i=1;i<len;i++){
dp[i][0] = Math.min(dp[i-1][1],dp[i-1][2]) + costs[i][0];
dp[i][1] = Math.min(dp[i-1][0],dp[i-1][2]) + costs[i][1];
dp[i][2] = Math.min(dp[i-1][1],dp[i-1][0]) + costs[i][2];
}
return Math.min(dp[len-1][0],Math.min(dp[len-1][1],dp[len-1][2]));
}
}
边栏推荐
- Is it safe to open a stock account online in 2022? Is there any danger?
- [paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
- Button button clear border
- leetcode:111. Minimum depth of binary tree
- What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
- 内存泄漏定位工具之 valgrind 使用
- .NET 5.0+ 无需依赖第三方 原生实现定时任务
- bash: ln: command not found
- CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
- Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
猜你喜欢
北汽蓝谷:业绩承压,极狐难期
CRC 校验
[encounter Django] - (II) database configuration
新一代云原生数据库的设计与实践
Uncover the secrets of new products! Yadi Guanneng 3 multi product matrix to meet the travel needs of global users
Sqlachemy common operations
[MPC] ① quadratic programming problem matlab solver quadprog
Rising stars in Plant Sciences (rsps2022) final Science Lecture (6.30 pm)
Mobile hard drive reads but does not display drive letter
Lack of comparator, operational amplifier to save the field! (the op amp is recorded as a comparator circuit)
随机推荐
SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
Recommend a JSON visualization tool artifact!
Dotnet console uses microsoft Maui. Getting started with graphics and skia
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
【邂逅Django】——(二)数据库配置
Valgrind usage of memory leak locating tool
关于#SQL#的问题,如何解决?
12. Gateway new generation gateway
使用强大的DBPack处理分布式事务(PHP使用教程)
2022年已经过去一半了,是不是很突然呢?
Japanese professor sues Intel FPGA and SOC products for infringing a design patent
内存泄漏定位工具之 valgrind 使用
SQL server2014 failed to delete the database, with an error offset of 0x0000
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
[encounter Django] - (II) database configuration
Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET
[MPC] ① quadratic programming problem matlab solver quadprog
MySQL common commands