当前位置:网站首页>LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改
LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改
2022-06-26 09:32:00 【Tisfy】
【LetMeFly】剑指 Offer II 091.粉刷房子 - 原地修改
力扣题目链接:https://leetcode.cn/problems/JEj789/
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的正整数矩阵 costs 来表示的。
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。
请计算出粉刷完所有房子最少的花费成本。
示例 1:
输入: costs = [[17,2,17],[16,16,5],[14,3,19]] 输出: 10 解释: 将 0 号房子粉刷成蓝色,1 号房子粉刷成绿色,2 号房子粉刷成蓝色。 最少花费: 2 + 5 + 3 = 10。
示例 2:
输入: costs = [[7,6,2]] 输出: 2
提示:
costs.length == ncosts[i].length == 31 <= n <= 1001 <= costs[i][j] <= 20
注意:本题与主站 256 题相同:https://leetcode-cn.com/problems/paint-house/
方法一:动态规划
这是一道比较容易想出来的动态规划,我们用 d p [ i ] [ j ] dp[i][j] dp[i][j]表示第 i + 1 i + 1 i+1个方块粉刷第 j j j个颜色时,前 i + 1 i + 1 i+1个方块儿的最小花费。
那么, m i n d p [ n − 1 ] [ 0 , 1 , 2 ] min{dp[n - 1][0, 1, 2]} mindp[n−1][0,1,2]就是答案(把第 n n n个方块粉刷成 3 3 3种颜色中的一个,前 n n n个方块的最小花费)
但是,相邻两个方块颜色不能相同。因此递推公式:
- 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]
如果允许修改 c o s t s costs costs数组,那么我们可以直接用 c o s t s costs costs数组来代替 d p dp dp数组, 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])即可。
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是房子个数
- 空间复杂度:
- 如果能修改 c o s t s costs costs数组,就不需要额外开辟数组空间,只需要使用常数个变量即可,此时空间复杂度为 O ( 1 ) O(1) O(1);
- 如果不能修改 c o s t s costs costs数组,那么 如果额外开辟一个 d p dp dp数组,空间复杂度为 O ( n ) O(n) O(n);如果使用 3 3 3个变量代替 d p dp dp数组,空间复杂度 O ( 1 ) O(1) O(1)(因为dp数组第 i − 1 i-1 i−1之前内容不会再用到)
AC代码
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]));
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125456885
边栏推荐
- Behavior tree file description
- mysql 数据库字段查询区分大小写设置
- Common SQL add / delete / modify query statements
- Is it safe to dig up money and make new debts
- xsync同步脚本的创建及使用(以Debian10集群为例)
- 【AAAI 2021】Few-Shot One-Class Classification via Meta-Learning 【FSOCC via Meta-learning】
- Pycharm occasionally encounters low disk space
- 0 basic how to make a cool leadership cockpit?
- install realsense2: The following packages have unmet dependencies: libgtk-3-dev
- 《一周搞定模电》—功率放大器
猜你喜欢

Single sign on logic

Merrill Lynch data helps State Grid Hubei "golden eye" accurately identify abnormal power consumption

《一周搞定模电》—集成运算放大器

【轨迹规划】Ruckig库的测试

"One week's work on Analog Electronics" - power amplifier

The first techo day Tencent technology open day, 628
Optimization of power assisted performance of QPM suspended window

Summary of common commands of vim

Flutter's brain map notes are easy to find and search!

"One week to finish the model electricity" - 55 timer
随机推荐
《一周搞定模电》—负反馈
"One week to finish the model electricity" - 55 timer
MATLAB basic operation command
首期Techo Day腾讯技术开放日,628等你
【Sensors 2021】Relation-Based Deep Attention Network with Hybrid Memory for One-Shot Person Re-Id
"One week's data collection" -- combinational logic circuit
Pycharm [debug] process stuck
Kubernetes cluster deployment (v1.23.5)
Edit type information
計算領域高質量科技期刊分級目錄
Analysis of ROS calculation diagram level
Principe et application du micro - ordinateur à puce unique - Aperçu
Solve Django's if Version (1, 3, 3): raise improverlyconfigured ('mysqlclient 1.3.3 or new is required
PHP does not allow images to be uploaded together with data (no longer uploading images before uploading data)
install ompl.sh
[open source] use phenocv weedcam for more intelligent and accurate weed management
OpenCV depthframe -> pointcloud 导致 segmentation fault!
工企专利匹配数据(数十万数据量)1998-2014年
十万行事务锁,开了眼界了。
【pulsar学习】pulsar架构原理