当前位置:网站首页>剑指 Offer II 091. 粉刷房子
剑指 Offer II 091. 粉刷房子
2022-07-03 08:49:00 【Sasakihaise_】
【DP】开一个dp[n+1][3]的数组,对于第i个房子的第j中颜色,可以有第i-1个房子的两外两种颜色的最小值得到。
class Solution {
//dp 1:21
public int minCost(int[][] costs) {
int n = costs.length;
int max = 1 << 30, ans = max;
int[][] dp = new int[n + 1][3];
for(var i = 1; i <= n; i++){
for(var j = 0; j < 3; j++){
int min = max;
for(var k = 0; k < 3; k++){
if(k == j) continue;
else min = Math.min(min, dp[i - 1][k]);
}
dp[i][j] = min + costs[i - 1][j];
if(i == n){
ans = Math.min(dp[i][j], ans);
}
}
}
return ans;
}
}
边栏推荐
- DOM 渲染系统(render mount patch)响应式系统
- On the difference and connection between find and select in TP5 framework
- 请求参数的发送和接收
- Unity notes 1
- XPath实现XML文档的查询
- Baidu editor ueeditor changes style
- Arbre DP acwing 285. Un bal sans patron.
- 【Rust 笔记】10-操作符重载
- [concurrent programming] consistency hash
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
猜你喜欢
Binary to decimal, decimal to binary
Parameters of convolutional neural network
UE4 source code reading_ Mobile synchronization
Es8 async and await learning notes
Binary tree sorting (C language, char type)
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
注解简化配置与启动时加载
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Unity editor expansion - controls, layouts
UE4 source code reading_ Bone model and animation system_ Animation compression
随机推荐
[rust notes] 07 structure
How to delete CSDN after sending a wrong blog? How to operate quickly
【Rust 笔记】10-操作符重载
单调栈-503. 下一个更大元素 II
How to deal with the core task delay caused by insufficient data warehouse resources
22-05-26 西安 面试题(01)准备
Allocation exception Servlet
Facial expression recognition based on pytorch convolution -- graduation project
php public private protected
DOM render mount patch responsive system
Downward compatibility and upward compatibility
【Rust笔记】05-错误处理
20220630学习打卡
Binary tree sorting (C language, int type)
【Rust 笔记】07-结构体
Arbre DP acwing 285. Un bal sans patron.
C language file reading and writing
[rust notes] 02 ownership
Unity multi open script
XPath实现XML文档的查询