当前位置:网站首页>LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路
LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路
2022-06-25 03:54: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]));
}
};
边栏推荐
- Hello CTP (II) -- Introduction to CTP
- 代表多样性的彩色 NFT 系列上线 The Sandbox 市场平台
- Crawler grabs the idea of reading on wechat
- MySQL插入过程报错1062,但是我没有该字段。
- web3.eth. Getpendingtransactions() returns null?
- Development of trading system (VII) -- Analysis of trading delay
- Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
- 【LeetCode】22. bracket-generating
- "Comment positionner l'industrie" dans la planification industrielle locale / parc
- 地方/園區產業規劃之 “ 如何進行產業定比特 ”
猜你喜欢

2022-06-21-Flink-49(一. SQL手册)

Deveco studio 3.0 editor configuration tips

数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁

Hot and cold, sweet and sour, want to achieve success? Dengkang oral, the parent company of lengsuanling, intends to be listed on the main board of Shenzhen Stock Exchange

(ultra detailed onenet TCP protocol access) arduino+esp8266-01s accesses the Internet of things platform, uploads real-time collected data /tcp transparent transmission (and how to obtain and write Lu

AI quantitative transaction (II) -- tushare financial data framework
![[harmony OS] [ark UI] basic ETS context operations](/img/fb/a1b8463ba160e6c5aa23d671a0c245.png)
[harmony OS] [ark UI] basic ETS context operations

Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.

PHP代码审计1—PHP.ini的那些事

讲座记录《多种空间大地测量技术的数据处理方法和应用》
随机推荐
Upgrade cmake
client-go gin的简单整合十-Update
PHP代码审计1—PHP.ini的那些事
Development of trading system (XIII) -- Analysis of quickfix source code
How to quickly deliver high-value software
MySQL modifies and deletes tables in batches according to the table prefix
Do you really need automated testing?
[harmony OS] [ark UI] basic ETS context operations
Flutter Builder & FutureBuilder组件
Comprehensive operation of financial risk management X of Dongcai
【LeetCode】22. bracket-generating
"Renaissance" in the digital age? The bottom digital collection makes people happy and sad
"Grammar sugar" -- my new programming knowledge
Standing wave ratio calculation method
Development of trading system (VIII) -- Construction of low delay network
2.吹响半音阶口琴
数学分析_笔记_第3章:极限
Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
opencv最大能打开多少图像?
Teach you how to install win11 system in winpe