当前位置:网站首页>LeetCode. One question of the day: offer II 091 Paint the house (DP problem)
LeetCode. One question of the day: offer II 091 Paint the house (DP problem)
2022-07-01 10:47:00 【To the light】
The finger of the sword Offer II 091. Paint the house
If there is a row of houses , common n individual , Every house can be painted red 、 One of the three colors blue or green , You need to paint all the houses and make the two adjacent houses not the same color .
Of course , Because the prices of different colors of paint on the market are different , So the cost of painting the house in different colors is different . The cost of painting each house in a different color is one n x 3 Positive integer matrix of costs To represent the .
for example ,costs[0][0] It means the first one 0 The cost of painting the house red ;costs[1][2] It means the first one 1 The cost of painting the house green , And so on .
Please calculate the minimum cost of painting all the houses .
Example 1:
Input : costs = [[17,2,17],[16,16,5],[14,3,19]]
Output : 10
explain : take 0 No. 1 house painted blue ,1 No. 1 house painted green ,2 No. 1 house painted blue .
Minimum cost : 2 + 5 + 3 = 10.
Example 2:
Input : costs = [[7,6,2]]
Output : 2
Tips :
costs.length == n
costs[i].length == 3
1 <= n <= 100
1 <= costs[i][j] <= 20
Solution:
- Here is the obvious dynamic programming problem , What we require is the minimum cost of painting all the houses , And we know , You give No i When painting a house, it is only related to the color of the previous house next to it , So it can be seen that the change of state lies in the color of two adjacent houses .
- So we might as well set
dp[i][j]Before painting i Houses , And the first i Paint a house j The minimum cost of color , So we just need to getdp[n-1][0],dp[n-1][1],dp[n-1][2]The minimum value in is enough , Get painted 0~n-1 Houses , And the last house is painted red , Blue , The minimum cost of green , Take min.
So clearly , According to our agreement that adjacent houses cannot be painted with the same color , You can cycle through the house , Transfer the painting state every time , Take the minimum cost every time .
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]));
}
}
边栏推荐
- Handling distributed transactions with powerful dbpack (PHP tutorial)
- mysql cdc能把能把op字段拿出来吗
- flutter Uint8List格式的图片和File格式图片的互相转换
- 使用强大的DBPack处理分布式事务(PHP使用教程)
- Kotlin 协程调度切换线程是时候解开真相了
- [MPC] ① quadratic programming problem matlab solver quadprog
- venv: venv 的目录结构
- Project0:小游戏
- [matytype] insert MathType inter line and intra line formulas in CSDN blog
- [MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
猜你喜欢

What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it

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

C one line code calculates the MD5 value of the file - codeplus series
![[matytype] insert MathType inter line and intra line formulas in CSDN blog](/img/ff/871a3f06f898ed107a2a974d2c7bc4.png)
[matytype] insert MathType inter line and intra line formulas in CSDN blog

venv: venv 的目录结构

价值1000毕业设计校园信息发布平台网站源码

数据库实验报告(一)

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

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

数据库实验报告(二)
随机推荐
12.Gateway新一代网关
CRC 校驗
[MPC] ① quadratic programming problem matlab solver quadprog
A new round of popularity of digital collections opens
Button button clear border
基金管理人的合规管理
flutter path_provider: ^2.0.10可以获取临时目录
678. 有效的括号字符串
Kotlin 协程调度切换线程是时候解开真相了
Wireshark TS | 快速重传和乱序之混淆
[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
基金管理人的内部控制
SQLAchemy 常用操作
C [byte array] and [hexadecimal string] mutual conversion - codeplus series
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
商城小程序源码开源版-可二开
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
How do clients request databases?
想开个户,在网上开华泰证券的户安全吗?
Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET