当前位置:网站首页>剑指 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]));
}
边栏推荐
猜你喜欢

限流设计及实现

Development of NFT for digital collection platform

Deep learning: numpy

xlua获取ugui的button注册click事件

Tiktok practice ~ sharing module ~ short video download (save to photo album)

Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用

Installation and use of logstash

阿里云个人镜像仓库日常基本使用

商品秒杀系统

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
随机推荐
Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
Redis single sign on system + voting system
Tiktok practice ~ homepage video ~ pull-down refresh
Good thing recommendation: mobile terminal development security tool
Tiktok practice ~ sharing module ~ short video download (save to photo album)
[recommended collection] these 8 common missing value filling skills must be mastered
IK分词器
Unity - URP get camera stack
Unity——Mathf. Similarities and differences between atan and atan2
SSO微服务工程中用户行为日志的记录
抖音实战~分享模块~复制短视频链接
Feign remote call
BOM and DOM operations
Successfully solved the problem of garbled microservice @value obtaining configuration file
String string is converted to jsonarray and parsed
When are global variables initialized before entering the main function?
Request method 'POST' not supported
Development of NFT for digital collection platform
Commodity seckill system
IK word breaker