当前位置:网站首页>【动态规划】剑指 Offer II 091. 粉刷房子
【动态规划】剑指 Offer II 091. 粉刷房子
2022-06-26 16:56:00 【不写博客就不爽】
剑指 Offer II 091. 粉刷房子
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的正整数矩阵 costs 来表示的。
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。
请计算出粉刷完所有房子最少的花费成本。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/JEj789
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:解决这道题目,一开始自己错误的使用贪心做法,后来发现如果每次贪心,结果取每个房间的最小值,出现错误结果
正确解法:动态规划
step1、dp[i][j] 表示 [0,i-1]间房子,i-1 间使用涂色 j,最小成本是 dp[i][j]
step2、递推公式
dp[i][j] = min(dp[i-1][m]) + costs[i-1][j], 要求m != j
step3、初始化
step4、递推顺序,从左到右
step5、举例子
代码实现
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]);
}
边栏推荐
- Calculate the sum of the main diagonals of the array
- 进军AR领域,这一次罗永浩能成吗?
- Jouer avec Linux et installer et configurer MySQL facilement
- Platform management background and merchant menu resource management: access control design of platform management background
- Byte interview: two array interview questions, please accept
- Use FST JSON to automatically generate faster JSON serialization methods
- Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
- 108. simple chat room 11: realize client group chat
- Teach you to learn dapr - 3 Run the first with dapr Net program
猜你喜欢

Jouer avec Linux et installer et configurer MySQL facilement

Incomplete line spacing adjustment of formula display in word

No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete

Platform management background and merchant menu resource management: Design of platform management background data service

Here comes the hero League full skin Downloader

【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测

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

Environment setup mongodb

Cache breakdown! Don't even know how to write code???

Army chat -- registration of Registration Center
随机推荐
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
She said she was tired! So tired! I want to change my career
Platform management background and merchant menu resource management: access control design of platform management background
Swap two numbers
Demonstrate to Xiaobai the case of sub database and sub table
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
Redis 概述整理
Concurrent thread safety
Some explanations for latex CJK
Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
Stm32f103c8t6 realize breathing lamp code
Leetcode HOT100 (22--- bracket generation)
【万字总结】以终为始,详细分析高考志愿该怎么填
并发之线程安全
Day10 daily 3 questions (2): count the number of the largest groups
Experience in hierarchical debugging of boards and cards
Teach you to learn dapr - 1 The era of net developers
Notes on flowus
Count the number of words in a line of string and take it as the return value of the function