当前位置:网站首页>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 the securities account opened by Yixue school for individuals safe? Is there a routine
- 建议收藏 | 在openGauss上遇到慢SQL该怎么办?
- Sqlachemy common operations
- 推荐一款 JSON 可视化工具神器!
- 442. 数组中重复的数据
- Handling distributed transactions with powerful dbpack (PHP tutorial)
- Suggest collecting | what to do when encountering slow SQL on opengauss?
- 华为HMS Core携手超图为三维GIS注入新动能
- 新一代云原生数据库的设计与实践
- Is it safe to buy funds on the access letter?
猜你喜欢

Venv: directory structure of venv

Matplotlib数据可视化基础

Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)

. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks

移动硬盘驱动器读到,但不显示盘符

Suggest collecting | what to do when encountering slow SQL on opengauss?

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

程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...

零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)

12款大家都在用的产品管理平台
随机推荐
缺少比较器,运放来救场!(运放当做比较器电路记录)
442. duplicate data in array
推荐一款 JSON 可视化工具神器!
A new round of popularity of digital collections opens
NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection
Can MySQL CDC take out the op field
Design and practice of new generation cloud native database
[matytype] insert MathType inter line and intra line formulas in CSDN blog
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
How to solve the problem of SQL?
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;
The list of winners of the digital collection of "century master" was announced
678. 有效的括号字符串
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul
Is it safe to open a stock account online in 2022? Is there any danger?
IDEA运行报错Command line is too long. Shorten command line for...
[MPC] ① quadratic programming problem matlab solver quadprog
Is it safe to do fund fixed investment on CICC securities?
想开个户,在网上开华泰证券的户安全吗?
Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years