当前位置:网站首页>剑指 Offer II 091. 粉刷房子
剑指 Offer II 091. 粉刷房子
2022-06-26 19:47:00 【北_尘】
看了下leetcode的今天的 每日一题,题目链接:剑指 Offer II 091. 粉刷房子
题目如下:
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的正整数矩阵 costs 来表示的。
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。
请计算出粉刷完所有房子最少的花费成本。
我这里使用动态规划的方法,方程如下:
1.初始化dp数组
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
2.状态转移方程:i的区间[1,n)
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];
整体代码如下:
public int minCost(int[][] costs) {
int n = costs.length;
int len = costs[0].length;
int[][] dp = new int[n][len];
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for(int i = 1;i < n;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[n-1][0],Math.min(dp[n-1][1],dp[n-1][2]));
}
当然也可改改三目运算符试试。
代码稍作改动,试着节省点空间,代码如下:
public int minCost(int[][] costs) {
int n = costs.length;
int len = costs[0].length;
int[][] dp = new int[2][len];
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for(int i = 1;i < n;i++){
dp[1][0] = Math.min(dp[0][1],dp[0][2])+costs[i][0];
dp[1][1] = Math.min(dp[0][0],dp[0][2])+costs[i][1];
dp[1][2] = Math.min(dp[0][0],dp[0][1])+costs[i][2];
dp[0][0] = dp[1][0];
dp[0][1] = dp[1][1];
dp[0][2] = dp[1][2];
dp[1][0] = 0;
dp[1][0] = 0;
dp[1][0] = 0;
}
return Math.min(dp[0][0],Math.min(dp[0][1],dp[0][2]));
}
边栏推荐
- MySQL stored procedure
- 商品秒杀系统
- [MySQL series] collection of common working SQL (continuous update)
- Create a time blocker yourself
- To: Apple CEO Cook: great ideas come from constantly rejecting the status quo
- 微服务版单点登陆系统(SSO)
- Developer survey: rust/postgresql is the most popular, and PHP salary is low
- Tiktok practice ~ homepage video ~ pull-down refresh
- 问题解决:虚拟机无法复制粘贴文件
- Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
猜你喜欢

Uni app uses canvas to draw QR code

Kubernetes 资源拓扑感知调度优化

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

Tiktok practice ~ search page ~ video details

Unity——Mathf. Similarities and differences between atan and atan2

论数据库的传统与未来之争之溯源溯本----AWS系列专栏

关于Qt数据库开发的一些冷知识

IK word breaker

MySQL - database creation and management

Basic and necessary common plug-ins of vscade
随机推荐
Wechat applet custom pop-up components
String string is converted to jsonarray and parsed
50 lines of code to crawl TOP500 books and import TXT documents
链游开发成品源码 链游系统开发详情说明
Boot的单元测试
Feign remote call
Tiktok practice ~ sharing module ~ copy short video link
Résumé des points de connaissance
Redis单点登陆系统+投票系统
[MySQL series] collection of common working SQL (continuous update)
460million zongzi were sold in half a year. How big is the "imagination space" of time-honored brands?
Logstash安装及使用
项目实战四:用户登录及token访问验证(reids+jwt)
【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
xlua获取ugui的button注册click事件
Redis single sign on system + voting system
股票开户的具体步骤是什么?网上开户安全吗?
Summary of several common UML diagrams
Is it safe to open an account for CICC Wealth Online?
Nftgamefi chain game system development detailed solution - chain game system development principle analysis