当前位置:网站首页>Leetcode refers to offer II 091 Paint house - modify in place
Leetcode refers to offer II 091 Paint house - modify in place
2022-06-26 09:44:00 【Tisfy】
【LetMeFly】 The finger of the sword Offer II 091. Paint the house - Modify in place
Force button topic link :https://leetcode.cn/problems/JEj789/
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 == ncosts[i].length == 31 <= n <= 1001 <= costs[i][j] <= 20
Be careful : This topic and the main station 256 The question is the same :https://leetcode-cn.com/problems/paint-house/
Method 1 : Dynamic programming
This is a dynamic programming that is easy to think of , We use it d p [ i ] [ j ] dp[i][j] dp[i][j] It means the first one i + 1 i + 1 i+1 Square painting No j j j When colors , front i + 1 i + 1 i+1 The minimum cost of a square .
that , m i n d p [ n − 1 ] [ 0 , 1 , 2 ] min{dp[n - 1][0, 1, 2]} mindp[n−1][0,1,2] That's the answer. ( The first n n n Paint a square into 3 3 3 One of the colors , front n n n The minimum cost of a square )
however , Two adjacent squares cannot have the same color . therefore The recursive formula :
- d p [ i ] [ 0 ] = m i n ( d p [ i − 1 ] [ 1 ] , d p [ i − 1 ] [ 2 ] ) + c o s t s [ i ] [ 0 ] dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + costs[i][0] dp[i][0]=min(dp[i−1][1],dp[i−1][2])+costs[i][0]
- d p [ i ] [ 1 ] = m i n ( d p [ i − 1 ] [ 0 ] , d p [ i − 1 ] [ 2 ] ) + c o s t s [ i ] [ 1 ] dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + costs[i][1] dp[i][1]=min(dp[i−1][0],dp[i−1][2])+costs[i][1]
- d p [ i ] [ 2 ] = m i n ( d p [ i − 1 ] [ 0 ] , d p [ i − 1 ] [ 1 ] ) + c o s t s [ i ] [ 2 ] dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + costs[i][2] dp[i][2]=min(dp[i−1][0],dp[i−1][1])+costs[i][2]
If modification is allowed c o s t s costs costs Array , So we can use it directly c o s t s costs costs Array instead of d p dp dp Array , c o s t s [ i ] [ j ] + = m i n ( c o s t s [ i − 1 ] [ x x ] ) costs[i][j] += min(costs[i - 1][xx]) costs[i][j]+=min(costs[i−1][xx]) that will do .
- Time complexity O ( n ) O(n) O(n), among n n n It's the number of houses
- Spatial complexity :
- If it can be modified c o s t s costs costs Array , There is no need to open up additional array space , Just use constant variables , At this time, the space complexity is O ( 1 ) O(1) O(1);
- If you can't modify c o s t s costs costs Array , that If you open an extra d p dp dp Array , The space complexity is O ( n ) O(n) O(n); If you use 3 3 3 Variables instead of d p dp dp Array , Spatial complexity O ( 1 ) O(1) O(1)( because dp Array number i − 1 i-1 i−1 The previous content will not be used again )
AC Code
C++
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
for (int i = 1; i < costs.size(); i++) {
costs[i][0] += min(costs[i - 1][1], costs[i - 1][2]);
costs[i][1] += min(costs[i - 1][0], costs[i - 1][2]);
costs[i][2] += min(costs[i - 1][0], costs[i - 1][1]);
}
return min(costs[costs.size() - 1][0], min(costs[costs.size() - 1][1], costs[costs.size() - 1][2]));
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125456885
边栏推荐
- mysql 数据库字段查询区分大小写设置
- 同花顺炒股软件安全性怎样?在同花顺怎么开户
- Thinking before QPM preparation optimization
- There is a strong demand for enterprise level data integration services. How to find a breakthrough for optimization?
- Explained: A Style-Based Generator Architecture for GANs (StyleGAN)
- Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
- How to correctly open the USB debugging and complete log functions of Huawei mobile phones?
- online trajectory generation
- Board end power hardware debugging bug
- thinkphp5手动报错
猜你喜欢

Cancellation and unbinding of qiniu cloud account

Several connection query methods of SQL (internal connection, external connection, full connection and joint query)

3大问题!Redis缓存异常及处理方案总结

"One week's study of model electricity" - capacitor, triode, FET

"One week's work on Analog Electronics" - integrated operational amplifier

【AAAI 2021】Few-Shot One-Class Classification via Meta-Learning 【FSOCC via Meta-learning】

【CVPR 2021】Joint Generative and Contrastive Learning for Unsupervised Person Re-identification

"One week's data collection" -- combinational logic circuit

Edge computing is the sinking and extension of cloud computing capabilities to the edge and user sides

How to view the data mini map quickly and conveniently after importing data in origin
随机推荐
install ompl. sh
halcon 光度立体
首期Techo Day腾讯技术开放日,628等你
PHP extracts TXT text to store the domain name in JSON data
Flutter's brain map notes are easy to find and search!
Record a time when the server was taken to mine
Jz2440--- using uboot burning program
SQL 函数
LeetCode 958. 二叉树的完全性校验
Shared by Merrill Lynch data technology expert team, smoking detection related practice based on Jetson nano
Solve Django's if Version (1, 3, 3): raise improverlyconfigured ('mysqlclient 1.3.3 or new is required
异常记录-23
做测试需要知道的内容——url、弱网、接口、自动化、
Creation and use of XSync synchronization script (taking debian10 cluster as an example)
2021-11-29 轨迹规划五次多项式
我在中山,到哪里开户比较好?在线开户安全么?
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
【CVPR 2019】Semantic Image Synthesis with Spatially-Adaptive Normalization(SPADE)
Thinkphp5 using the composer installation plug-in prompts that the PHP version is too high
Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID