当前位置:网站首页>【动态规划】剑指 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]);
}
边栏推荐
- 数字藏品与NFT到底有何区别
- 108. simple chat room 11: realize client group chat
- Calculate the average of N numbers in the index group of X, and return the number that is less than the average and closest to the average through formal parameters
- Web3 decentralized storage ecological landscape
- Toupper function
- Distributed Architecture Overview
- Redis 概述整理
- The function keeps the value of variable H to two decimal places and rounds the third digit
- Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
- Programmer's essential toolkit, please collect!
猜你喜欢
![[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

Web3去中心化存储生态图景

She said she was tired! So tired! I want to change my career

ACL 2022 | zero sample multilingual extracted text summarization based on neural label search

Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation

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

Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)

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

Teach you to learn dapr - 8 binding
随机推荐
What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
Web3 decentralized storage ecological landscape
Fire evacuation and self rescue... This safety production and fire training is full!
量化合约系统开发分析案例丨合约量化系统开发方案详解
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
Getting started with mongodb
玩轉Linux,輕松安裝配置MySQL
关于FlowUs这一款国民好笔记
Viewing the task arrangement ability of monorepo tool from turborepo
[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion
Microservice architecture practice: business management background and SSO design: SSO design
Community ownership of NFT trading market is unstoppable
Necessary decorator mode for 3 years' work
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Day10 daily 3 questions (2): count the number of the largest groups
Discussion: the next generation of stable coins
Introduction to distributed cache / cache cluster
Memory partition model
Record the use process of fenics
Fgetc() reads content from file