当前位置:网站首页>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]));
}
}
边栏推荐
- mysql cdc能把能把op字段拿出来吗
- Kotlin coprocessor scheduling switch threads it's time to unravel the truth
- 【MPC】①二次规划问题MATLAB求解器quadprog
- [paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
- [.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
- 移动硬盘驱动器读到,但不显示盘符
- 基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
- Project0:小游戏
- 使用强大的DBPack处理分布式事务(PHP使用教程)
- 云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
猜你喜欢

SQL server2014 failed to delete the database, with an error offset of 0x0000

Lack of comparator, operational amplifier to save the field! (the op amp is recorded as a comparator circuit)

12.Gateway新一代网关

venv: venv 的目录结构

Who's still buying three squirrels
![[paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba](/img/fa/f2d24ee3dbbbe6332c84a82109338e.png)
[paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba

Addition, deletion, modification and query of database

Sqlachemy common operations

数字藏品市场新局面

Kotlin 协程调度切换线程是时候解开真相了
随机推荐
leetcode:111. Minimum depth of binary tree
使用强大的DBPack处理分布式事务(PHP使用教程)
【MPC】①二次规划问题MATLAB求解器quadprog
SQL optimization - in and not in, exist
Is it safe to buy funds on the access letter?
缺少比较器,运放来救场!(运放当做比较器电路记录)
客户端如何请求数据库?
基金管理人的内部控制
Have the bosses ever done the operation of sink shunting and writing to Clickhouse or other databases.
I'd like to know where I can open an account in Guangzhou? Is it safe to open an account online now?
建议收藏 | 在openGauss上遇到慢SQL该怎么办?
Common penetration tools -goby
[MPC] ① quadratic programming problem matlab solver quadprog
华为HMS Core携手超图为三维GIS注入新动能
CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
Guys, how to export iceberg data to MySQL? What tools are there? Neither sqoop nor dataX
bash: ln: command not found
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
How do clients request databases?
[laravel] detailed explanation of faker data filling