当前位置:网站首页>LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
2022-06-25 04:15:00 【HERODING23】

解題思路:
非常基礎的一道動態規劃題,甚至可以直接在題中所給的costs基礎上進行,把costs作為狀態轉移數組,cost[i][0]顯然是由之前i-1的1和2顏色决定的,cost[i][1]和cost[i][2]也是同理,所以一直取每個狀態的最小和,返回即可,代碼如下:
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
int n = costs.size();
for(int i = 1; i < n; i ++) {
costs[i][0] += min(costs[i-1][1], costs[i-1][2]);
costs[i][1] += min(costs[i-1][0], costs[i-1][2]);
costs[i][2] += min(costs[i-1][0], costs[i-1][1]);
}
return min(costs[n-1][0], min(costs[n-1][1], costs[n-1][2]));
}
};
边栏推荐
- opencv怎么安装?opencv下载安装教程
- opencv是开源的吗?
- Development of trading system (III) - risk control system
- PHP code audit 2 - these functions must be known and understood
- Hello CTP (IV) - CTP transaction API
- [kubernetes series] installation and use of Helm
- SEO的5大关键指标:排名+流量+会话+停留时长+跳出率
- Cesium graphic annotation circle, square, polygon, ellipse, etc
- 2022-06-21-Flink-49(一. SQL手册)
- Although the Internet in the traditional sense has long ceased to exist, this does not mean that the Internet has long disappeared
猜你喜欢

论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》

【组队学习】SQL编程语言笔记——Task04

Crawler grabs the idea of reading on wechat

《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台

练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。

Acmstreamopen return value problem

《悉达多》:一生之书,可以时常反刍

Serious PHP defects can lead to rce attacks on QNAP NAS devices
![[openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.](/img/62/6152d5a30c92a340cb286c7b1cbc54.png)
[openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.

Is opencv open source?
随机推荐
文本关键词提取:ansj
client-go gin的简单整合十一-Delete
Development of trading system (XII) - Official quickfix document
web3.eth. Getpendingtransactions() returns null?
2022-06-21-Flink-49(一. SQL手册)
Crawler crawls Sina Weibo data
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
Jilin University 22 spring March new development English comprehensive course (I) assignment assessment-00080
General steps for QT compiling database plug-ins
Hello CTP (IV) - CTP transaction API
Hello CTP (III) - CTP quotation API
智慧风电:数字孪生 3D 风机智能设备运维
How many images can opencv open?
佐喃社区
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00005
Jilin University 22 spring March "technical economics" assignment assessment-00073
Watch out for the stolen face! So many risks of face recognition used every day?
虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
openmmlab-环境配置