当前位置:网站首页>[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]);
}
边栏推荐
- Implementation of MySQL master-slave architecture
- Count the number of each vowel letter in the string
- ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
- MySql 导出数据库中的全部表索引
- Toupper function
- Swap two numbers
- Detailed contract quantification system development scheme and technical description of quantitative contract system development
- Prometeus 2.34.0 新特性
- 20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
- [Error] ld returned 1 exit status
猜你喜欢

Introduction to minimal API

Kubernetes essential tools: 2021

Decentralized NFT transaction protocol will defeat opensea

【万字总结】以终为始,详细分析高考志愿该怎么填

Introduction to distributed cache / cache cluster

宝藏又小众的CTA动画素材素材网站分享

Teach you to learn dapr - 5 Status management

Web3 decentralized storage ecological landscape

Demonstrate to Xiaobai the case of sub database and sub table

Redis OM . Net redis object mapping framework
随机推荐
VSCode使用 - Remote-SSH 配置说明
Teach you to learn dapr - 9 Observability
国信证券怎么开户?通过链接办理股票开户安全吗
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
[Error] ld returned 1 exit status
Prometeus 2.34.0 新特性
Programmer's essential toolkit, please collect!
What does the equals method compare? Who told you
Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
num[i]++
玩转Linux,轻松安装配置MySQL
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
Calculate the sum of the main diagonals of the array
Platform management background and merchant menu resource management: access control design of platform management background
vue--vuerouter缓存路由组件
Detailed contract quantification system development scheme and technical description of quantitative contract system development
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
The student record consists of student number and academic performance. The data of n students have been stored in the a structure array to find out the student record with the lowest performance