当前位置:网站首页>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]));
}
};
边栏推荐
- 【LeetCode】143. 重排链表
- 1280_C语言求两个无符号整形的平均值
- Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
- [rust contribution] implement Message Oriented Middleware (6) -client from zero
- NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
- "How to carry out industrial positioning" in local / Park industrial planning
- BSC parsing input data of transaction
- ZABBIX installation pit avoidance Guide
- Lecture record: history and development of strapdown inertial navigation solution
- The art of writing simple code
猜你喜欢
随机推荐
The art of writing simple code
代表多样性的彩色 NFT 系列上线 The Sandbox 市场平台
Flutter Builder & FutureBuilder组件
WMS仓储管理系统的使用价值,你知道多少
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
[harmony OS] [ark UI] basic ETS context operations
讲座记录《多种空间大地测量技术的数据处理方法和应用》
Vigilance against over range collection of privacy - ten mobile app violations
Deveco studio 3.0 editor configuration tips
client-go gin的简单整合十-Update
小心被偷脸!天天用的人脸识别风险原来这么多?
学习码 滚动码 固定码 有什么区别重码数,编码容量滚动码的原理
PHP代码审计2—这些函数必知必会
Development of trading system (XI) -- Introduction to quickfix
Text keyword extraction: ansj
【LeetCode】148. 排序链表
How to draw an industry investment map
微信小程序中的列表渲染
On the self-cultivation of an excellent red team member
SEO的5大关键指标:排名+流量+会话+停留时长+跳出率