当前位置:网站首页>[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]);
}
边栏推荐
- 分布式架构概述
- Teach you to learn dapr - 1 The era of net developers
- 合约量化系统开发方案详细,量化合约系统开发技术说明
- Necessary decorator mode for 3 years' work
- LeetCode——226. 翻转二叉树(BFS)
- 去中心化NFT交易协议将击败OpenSea
- Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
- Notes on flowus
- SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
猜你喜欢

【推荐系统学习】推荐系统的技术栈

Teach you to learn dapr - 2 Must know concept

10 cloud security best practices that enterprises need to know

物联网协议的王者:MQTT

Notes on flowus

Redis OM . Net redis object mapping framework

Classical synchronization problem

ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

Interpretation of new plug-ins | how to enhance authentication capability with forward auth
随机推荐
LeetCode——226. 翻转二叉树(BFS)
分布式缓存/缓存集群简介
Cloud native 02: Alibaba cloud cloud efficient flow pipeline
When I was in the library, I thought of the yuan sharing mode
NFT 交易市场社区所有化势不可挡
探讨:下一代稳定币
Leetcode daily [2022 - 02 - 16]
Demonstrate to Xiaobai the case of sub database and sub table
Web3去中心化存储生态图景
Platform management background and merchant menu resource management: access control design of platform management background
[suggested collection] 11 online communities suitable for programmers
Microservice architecture practice: user login and account switching design, order query design of the mall
7 views on NFT market prospect
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
Comp281 explanation
分布式架构概述
vue--vuerouter缓存路由组件
Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Teach you to learn dapr - 9 Observability