当前位置:网站首页>Li Kou daily question sword finger offer II 091. paint the house
Li Kou daily question sword finger offer II 091. paint the house
2022-07-27 06:07:00 【The last tripod】
The finger of the sword Offer II 091. Paint the house 

A simple DP problem , But the data is too small , Feel that violence can be done , Although I didn't think about it
State shift :
At present, the minimum price of painting a certain color is the price of the cheaper one of the other two colors plus the price of the current color
State transition equation :
f[0][i] = Math.min(f[1][i-1], f[2][i-1]) + costs[i-1][0];
f[1][i] = Math.min(f[0][i-1], f[2][i-1]) + costs[i-1][1];
f[2][i] = Math.min(f[0][i-1], f[1][i-1]) + costs[i-1][2];
class Solution {
public int minCost(int[][] costs) {
int n = costs.length;
int[][] f = new int[3][n+10];
f[0][0] = f[1][0] = f[2][0] = 0;
for(int i = 1; i<=n; i++){
f[0][i] = Math.min(f[1][i-1], f[2][i-1]) + costs[i-1][0];
f[1][i] = Math.min(f[0][i-1], f[2][i-1]) + costs[i-1][1];
f[2][i] = Math.min(f[0][i-1], f[1][i-1]) + costs[i-1][2];
}
int min = Math.min(f[0][n],f[1][n]);
return Math.min(min,f[2][n]);
}
}
- Time complexity O(n)
- Spatial complexity O(n)// Because only the previous value is needed , Dynamic arrays can be used to make the space complexity be O(1)
Dynamic array optimization
class Solution {
int[][] f = new int[3][2];
void swap(){
for(int i=0; i<3; i++){
int tmp = f[i][0];
f[i][0] = f[i][1];
f[i][1] = tmp;
}
}
public int minCost(int[][] costs) {
int n = costs.length;
f[0][0] = f[1][0] = f[2][0] = 0;
for(int i = 1; i<=n; i++){
f[0][1] = Math.min(f[1][0], f[2][0]) + costs[i-1][0];
f[1][1] = Math.min(f[0]0], f[2][0]) + costs[i-1][1];
f[2][1] = Math.min(f[0][0], f[1][0]) + costs[i-1][2];
swap();
}
int min = Math.min(f[0][0],f[1][0]);
return Math.min(min,f[2][0]);
}
}
- Time complexity O(n)
- Spatial complexity O(1)// But actually , Because there is less data , The effect of this method is not as good as the above
边栏推荐
- Baiwen driving Daquan learning (II) I2C driving
- 【头歌】重生之我在py入门实训中(2):公式编程
- [first song] machine learning of rebirth - linear regression
- Baiwen driver Daquan learning (I) LCD driver
- 数据库索引的一些说明以及使用
- PZK学C语言之数据类型,进制转换,输入输出,运算符,分支语句ifelse
- 【头歌】重生之我在py入门实训中(6):函数的定义与应用
- 【头歌】重生之我在py入门实训中(1)
- [Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog
- 【11】二进制编码:“手持两把锟斤拷,口中疾呼烫烫烫”?
猜你喜欢

【Arduino】重生之Arduino 学僧(1)
![[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog](/img/75/8f41db9f9c077b43751d63b7b5b57e.png)
[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog

geonode geoserver win10 安装教程(亲测)
![[MySQL learning] 8](/img/25/84d5acbdd8aba3455ab8e3eb17dfa8.png)
[MySQL learning] 8

ps 2022 六月更新,都新增了哪些功能

Live Home 3D Pro室内家居设计工具

Can it replace PS's drawing software?

What tools are needed to make video post effects?

Greedy high performance neural network and AI chip application research and training

PS 2022 updated in June, what new functions have been added
随机推荐
STM32-FSMC外扩内存SRAM
std::bind与std::function的一些应用
【头歌】重生之我在py入门实训中(12):Matplotlib接口和常用图形
使用Markdowm
安装windows下的redis
C语言--字符串操作函数与内存操作函数
数据库索引的一些说明以及使用
剪枝-量化-转onnx中文系列教程
2022.6.10 STM32MP157串口时钟的学习
【头歌】重生之机器学习-线性回归
What tools are needed to make video post effects?
Greedy high performance neural network and AI chip application research and training
[song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
【11】二进制编码:“手持两把锟斤拷,口中疾呼烫烫烫”?
如何管理大量的定时任务
Unittest套件与运行器
编程学习记录——第3课【初识C语言】
Live Home 3D Pro室内家居设计工具
16. Over fitting and under fitting
芯片、存储器及其关键指标简述 一