当前位置:网站首页>[dynamic planning] Jianzhi offer II 091 Paint the house
[dynamic planning] Jianzhi offer II 091 Paint the house
2022-06-26 17:18:00 【Not blogging】
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/JEj789
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas : Solve this problem , At first, I mistakenly used greedy practices , Later, I found that if every time I was greedy , Results take the minimum value of each room , Wrong result
The correct solution : Dynamic programming
step1、dp[i][j] Express [0,i-1] A house ,i-1 Apply color between j, The minimum cost is dp[i][j]
step2、 The recursive formula
dp[i][j] = min(dp[i-1][m]) + costs[i-1][j], requirement m != j
step3、 initialization
step4、 Recursive order , From left to right
step5、 For example
Code implementation
public int minCost(int[][] costs) {
int n = costs.length;
int[][] dp = new int[n+1][3];
for(int i = 1; i <= n; i++){
dp[i][0] = Math.min(dp[i-1][1], dp[i-1][2]) + costs[i-1][0];
dp[i][1] = Math.min(dp[i-1][0], dp[i-1][2]) + costs[i-1][1];
dp[i][2] = Math.min(dp[i-1][0], dp[i-1][1]) + costs[i-1][2];
}
return Math.min(Math.min(dp[n][0], dp[n][1]), dp[n][2]);
}
边栏推荐
- Deployment and operation of mongodb partitioned cluster
- Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
- Calculate a=1, a2=1/1=a1
- 10 cloud security best practices that enterprises need to know
- Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
- y=1/100*100+1/200*200+1/300*300+.....+ 1/m*m
- 【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
- Basic requirements: 7 problems in singleton mode
- 分布式架构概述
- Turtle cartography
猜你喜欢

背包问题求方案数

sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)

Can Luo Yonghao succeed in entering the AR field this time?

Army chat -- registration of Registration Center

Basic requirements: 7 problems in singleton mode

Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack

Demonstrate to Xiaobai the case of sub database and sub table

Jouer avec Linux et installer et configurer MySQL facilement

Vue--vuerouter cache routing component

Community ownership of NFT trading market is unstoppable
随机推荐
20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)
Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
背包问题求方案数
SQL injection for Web Security (3)
玩转Linux,轻松安装配置MySQL
分布式架构概述
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
Leetcode topic [array] -268- missing numbers
Microservice architecture practice: user login and account switching design, order query design of the mall
Teach you to learn dapr - 1 The era of net developers
经典同步问题
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
Multiply the values of the upper triangular elements of the array by M
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
Leetcode daily [2022 - 02 - 16]
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
MySQL index
Community ownership of NFT trading market is unstoppable
Demonstrate to Xiaobai the case of sub database and sub table