当前位置:网站首页>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]));
}
}
边栏推荐
- Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
- 【Matytype】在CSDN博客中插入Mathtype行间与行内公式
- Handling distributed transactions with powerful dbpack (PHP tutorial)
- SQL SERVER2014删除数据库失败,报错偏移量0x0000...
- Valgrind usage of memory leak locating tool
- CRC 校驗
- Valgrind usage of memory leak locating tool
- 678. Valid bracket string
- Write your own who commands
- 【邂逅Django】——(二)数据库配置
猜你喜欢

建议收藏 | 在openGauss上遇到慢SQL该怎么办?

推荐一款 JSON 可视化工具神器!

Kotlin 协程调度切换线程是时候解开真相了

基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建

Simulink simulation circuit model of open loop buck buck buck chopper circuit based on MATLAB

【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba

A new round of popularity of digital collections opens

新一代云原生数据库的设计与实践

Half of 2022 has passed, isn't it sudden?

The list of winners of the digital collection of "century master" was announced
随机推荐
12款大家都在用的產品管理平臺
12 product management platforms that everyone is using
[MPC] ① quadratic programming problem matlab solver quadprog
建议收藏 | 在openGauss上遇到慢SQL该怎么办?
C# 一行代码计算文件的MD5值 - CodePlus系列
Crawler (2) - requests (1) | deep parsing of requests module
SQL optimization - in and not in, exist
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
A new round of popularity of digital collections opens
Superscalar processor design yaoyongbin Chapter 4 branch prediction -- Excerpt from subsection 4.1
云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
Prism journal navigation button usability exploration record
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
Half of 2022 has passed, isn't it sudden?
I'd like to know where I can open an account in Guangzhou? Is it safe to open an account online now?
客户端如何请求数据库?
Error: missing revert data in call exception
华为HMS Core携手超图为三维GIS注入新动能
venv: venv 的目录结构