当前位置:网站首页>LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子
LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子
2022-08-10 14:43:00 【Fitz1318】
题目链接
题目描述
假如有一排房子,共 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
解题思路
动态规划
确认dp数组及下标的含义
dp[i][k]:粉刷前i个房子且第i个房子被粉刷成第k中颜色时的最小花费成本
确定递推公式
- 如果第i个房子被粉刷成第0种颜色时,则
dp[i][0]可以由两个状态推导出来- 如果第i-1个房子被粉刷成第1种颜色,由于相邻的颜色不能一样,所以
dp[i][0] = dp[i-1][1] + costs[i][0] - 如果第i-1个房子被粉刷成第2种颜色,由于相邻的颜色不能一样,所以
dp[i][0] = dp[i-1][2] + costs[i][0] - 综上,
dp[i][0] = Math.min(dp[i-1][1],dp[i-1][2]) + costs[i][0]
- 如果第i-1个房子被粉刷成第1种颜色,由于相邻的颜色不能一样,所以
- 如果第i个房子被粉刷成第1种颜色时,则
dp[i][1]可以由两个状态推导出来- 如果第i-1个房子被粉刷成第0种颜色,由于相邻的颜色不能一样,所以
dp[i][1] = dp[i-1][0] + costs[i][1] - 如果第i-1个房子被粉刷成第2种颜色,由于相邻的颜色不能一样,所以
dp[i][0] = dp[i-1][2] + costs[i][1] - 综上,
dp[i][1] = Math.min(dp[i-1][0],dp[i-1][2]) + costs[i][1]
- 如果第i-1个房子被粉刷成第0种颜色,由于相邻的颜色不能一样,所以
- 如果第i个房子被粉刷成第2种颜色时,则
dp[i][2]可以由两个状态推导出来- 如果第i-1个房子被粉刷成第0种颜色,由于相邻的颜色不能一样,所以
dp[i][2] = dp[i-1][0] + costs[i][2] - 如果第i-1个房子被粉刷成第1种颜色,由于相邻的颜色不能一样,所以
dp[i][2] = dp[i-1][1] + costs[i][2] - 综上,
dp[i][2] = Math.min(dp[i-1][0],dp[i-1][1]) + costs[i][2]
- 如果第i-1个房子被粉刷成第0种颜色,由于相邻的颜色不能一样,所以
dp数组初始化
dp[0][k] = costs[0][k]
确定遍历顺序
- 从前往后遍历
AC代码
class Solution {
public int minCost(int[][] costs) {
int len = costs.length;
int[][] dp = new int[len][3];
for (int i = 0; i < 3; i++) {
dp[0][i] = costs[0][i];
}
for (int i = 1; i < len; i++) {
dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + costs[i][0];
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + costs[i][1];
dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + costs[i][2];
}
return Math.min(dp[len - 1][0], Math.min(dp[len - 1][1], dp[len - 1][2]));
}
}
边栏推荐
- Summary of tensorflow installation stepping on the pit
- MySQL advanced (thirty-three) MySQL data table adding fields
- TestLink导出用例转换工具
- “国资云”和“国家云”能给市场带来怎样的变革?
- 12海里、24海里、200海里的意义及名称
- XML基本学习
- 物资采购小程序开发制作功能介绍
- 2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
- Unfinished mathematics test paper ----- test paper generator (Qt includes source code)
- 2022-08-10 Daily: Swin Transformer author Cao Yue joins Zhiyuan to carry out research on basic vision models
猜你喜欢

MySQL - storage engine for databases

win2012安装Oraclerac失败

E. Cross Swapping(并查集变形/好题)

领域驱动模型设计与微服务架构落地-从项目去剖析领域驱动

Rich Dad Poor Dad Reading Notes

注意力模型---Attention Model

正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)

PyTorch multi-machine multi-card training: DDP combat and skills

Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"

Azure IoT 合作伙伴技术赋能工作坊:IoT Dev Hack
随机推荐
CSP-J1 CSP-S1 初赛 第1轮(2022.08.09)
How is the monthly salary table stored in the database?Ask for a design idea
司空见惯 - 股市狠狠下跌后,何時能反弹?
使用Uiautomator2进行APP自动化测试
静态变量存储在哪个区
兆骑科创高层次人才创业大赛平台,投融资对接,双创服务
Rich Dad Poor Dad Reading Notes
file system design
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
1004(树状数组+离线操作+离散化)
1004 (tree array + offline operation + discretization)
2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
宝塔面板开放Redis给指定外网机器
阿里五位MySQL封神大佬耗17个月总结出53章性能优化法则
leetcode 739. Daily Temperatures Daily Temperatures (Moderate)
消息称原美图高管加盟蔚来手机 顶配产品或超7000元
Alibaba的秒杀系统—千亿级并发设计手册上线了
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
“国资云”和“国家云”能给市场带来怎样的变革?
Understanding_Data_Types_in_Go