当前位置:网站首页>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]));
}
}
边栏推荐
- Japanese professor sues Intel FPGA and SOC products for infringing a design patent
- LeetCode.每日一题 剑指 Offer II 091. 粉刷房子 (DP问题)
- 【MPC】①二次规划问题MATLAB求解器quadprog
- 关于#SQL#的问题,如何解决?
- 新品大揭秘!雅迪冠能 3 多元产品矩阵,满足全球用户出行需求
- 零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)
- 想开个户,在网上开华泰证券的户安全吗?
- 678. Valid bracket string
- Want to open an account, is it safe to open an account of Huatai Securities online?
- 有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
猜你喜欢

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

Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched

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

使用强大的DBPack处理分布式事务(PHP使用教程)

CRC 校驗

CRC verification
![[MPC] ① quadratic programming problem matlab solver quadprog](/img/be/5e300255041e3348b933bc32e2ea46.png)
[MPC] ① quadratic programming problem matlab solver quadprog

数据库实验报告(二)

CRC check

Wireshark TS | 快速重传和乱序之混淆
随机推荐
Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
php 实现抽奖功能
CCNP Part XII BGP (IV)
Valgrind usage of memory leak locating tool
About database: how to avoid deadlock in gbase 8s
在通达信上买基金安全吗?
Prism journal navigation button usability exploration record
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;
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
中国探月工程独家藏品限量发售!
Does anyone know why? The table structure is the source table MySQL CDC that has just been directly copied
The list of winners of the digital collection of "century master" was announced
问下群里的各位,有使用flink oracle cdc的logminer方案,在生产上稳定运行的实际
venv: venv 的目录结构
使用强大的DBPack处理分布式事务(PHP使用教程)
. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks
Daily mathematics serial 55: February 24
Wireshark TS | 快速重传和乱序之混淆
个人商城二开逍遥B2C商城系统源码-可商用版/拼团拼购优惠折扣秒杀源码
Is the securities account opened by Yixue school for individuals safe? Is there a routine