当前位置:网站首页>【动态规划】剑指 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]);
}
边栏推荐
- Day10 daily 3 questions (2): count the number of the largest groups
- Wechat app mall, review products, upload commodity pictures, and score Commodity Services
- 【万字总结】以终为始,详细分析高考志愿该怎么填
- 经典同步问题
- Demonstrate to Xiaobai the case of sub database and sub table
- NFT 交易市场社区所有化势不可挡
- [C language] static modifies local variables
- Redis' 43 serial cannons, try how many you can carry
- Microservice architecture practice: business management background and SSO design: SSO design
- [recommendation system learning] recommendation system architecture
猜你喜欢

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)

SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用

Necessary decorator mode for 3 years' work

Classical synchronization problem

Leetcode HOT100 (22--- bracket generation)

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC

NFT 交易市场社区所有化势不可挡

Leetcode 1169. 查询无效交易(如果数据量不大,这种题还是得暴力枚举解决)

玩轉Linux,輕松安裝配置MySQL

vue--vuerouter缓存路由组件
随机推荐
Use FST JSON to automatically generate faster JSON serialization methods
Redis 概述整理
Classical synchronization problem
Concurrent thread safety
Can Luo Yonghao succeed in entering the AR field this time?
Swap two numbers
Toupper function
Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed
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!
num[i]++
Demonstrate to Xiaobai the case of sub database and sub table
Microservice architecture practice: user login and account switching design, order query design of the mall
Teach you to learn dapr - 6 Publish subscription
Count the number of words in a line of string and take it as the return value of the function
Teach you to learn dapr - 4 Service invocation
Leetcode daily [2022 - 02 - 16]
Experience in hierarchical debugging of boards and cards
5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
建立自己的网站(16)
Problems encountered this week