当前位置:网站首页>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]));
}
}
边栏推荐
- 442. 数组中重复的数据
- Write your own who commands
- 北汽蓝谷:业绩承压,极狐难期
- What legal risks and qualifications should be paid attention to when building a digital collection platform?
- 投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
- 【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba
- Handling distributed transactions with powerful dbpack (PHP tutorial)
- 12款大家都在用的产品管理平台
- Rising stars in Plant Sciences (rsps2022) final Science Lecture (6.30 pm)
- NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection
猜你喜欢

华为HMS Core携手超图为三维GIS注入新动能

零基础入门测试该学什么?最全整理,照着学就对了
![[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》](/img/b3/b117481fba7257453011e4cdb1eaaa.png)
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》

云上“视界” 创新无限 | 2022阿里云直播峰会正式上线

CRC 校驗

Have you learned the necessary global exception handler for the project

What a high commission! The new programmer's partner plan is coming. Everyone can participate!

【Matytype】在CSDN博客中插入Mathtype行间与行内公式

NC | 肠道细胞和乳酸菌共同作用来防止念珠菌感染
![[encounter Django] - (II) database configuration](/img/23/aed472757f7e238a146b043c0405d7.png)
[encounter Django] - (II) database configuration
随机推荐
MySQL common commands
[matytype] insert MathType inter line and intra line formulas in CSDN blog
SQL SERVER2014删除数据库失败,报错偏移量0x0000...
爬虫(2) - Requests(1) | Requests模块的深度解析
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
Who's still buying three squirrels
Error: missing revert data in call exception
C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
新品大揭秘!雅迪冠能 3 多元产品矩阵,满足全球用户出行需求
Superscalar processor design yaoyongbin Chapter 4 branch prediction -- Excerpt from subsection 4.1
Daily mathematics serial 55: February 24
[paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
Half of 2022 has passed, isn't it sudden?
C [byte array] and [hexadecimal string] mutual conversion - codeplus series
数字藏品平台搭建需要注意哪些法律风险及资质?
12 plateformes de gestion de produits utilisées par tout le monde
Project0:小游戏
Valgrind usage of memory leak locating tool
Matplotlib数据可视化基础
A new round of popularity of digital collections opens