当前位置:网站首页>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
边栏推荐
- Live Home 3D Pro室内家居设计工具
- 力扣题解 动态规划(1)
- socket编程一:使用fork()实现最基础的并发模式
- [first song] rebirth of me in py introductory training (6): definition and application of functions
- [first song] deep learning of rebirth -keras (elementary)
- Leetcode每日一题30. 串联所有单词的子串
- IOT operating system
- 编程学习记录——第7课【函数】
- Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
- 韦东山 数码相框 项目学习(二)在LCD上显示中文字符
猜你喜欢

Cesium教程 (1) 界面介绍-3dtiles加载-更改鼠标操作设置

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

16. Over fitting and under fitting

根据SQL必知必会学习SQL(MYSQL)

Essential tool for making video special effects: nuke 13
![[first song] machine learning of rebirth - linear regression](/img/70/3efd9eacf88f55022eb52d096926f7.png)
[first song] machine learning of rebirth - linear regression

PS 2022 updated in June, what new functions have been added
![[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

What tools are needed to make video post effects?

Live Home 3D Pro interior home design tool
随机推荐
百问网驱动大全学习(二)I2C驱动
Speech and Language Processing (3rd ed. draft) Chapter 2 ——正则表达式,文本归一化,编辑距离 阅读笔记
安装windows下的redis
常见的SQL优化方法
代码随想录笔记_哈希_242有效的字母异位词
[first song] deep learning of rebirth -keras (elementary)
个人开发者申请代码签名证书的签发流程
WebODM win10安装教程(亲测)
力扣题解 动态规划(7)
【头歌】重生之我在py入门实训中(2):公式编程
【5·20特辑】MatLAb之我在和你表白
数据库索引的一些说明以及使用
力扣第一周错题集
[first song] rebirth of me in py introductory training (6): definition and application of functions
【11】二进制编码:“手持两把锟斤拷,口中疾呼烫烫烫”?
韦东山 数码相框 项目学习(四)简易的TXT文档显示器(电纸书)
C语言-自定义结构类型
判断是否为回文结构的三种方法
arcgis for js api-入门系列
力扣题解 动态规划(3)