当前位置:网站首页>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]));
}
};
边栏推荐
- "How to carry out industrial positioning" in local / Park industrial planning
- JS tool function, self encapsulating a throttling function
- Intel 13代酷睿首次露出真面目:68MB缓存提升明显
- Lecture record: history and development of strapdown inertial navigation solution
- Cesium graphic annotation circle, square, polygon, ellipse, etc
- SEO的5大关键指标:排名+流量+会话+停留时长+跳出率
- 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
- Numpy NP tips: use OpenCV to interpolate and zoom the array to a fixed shape cv2 resize(res, dsize=(64, 64), interpolation=cv2. INTER_ CUBIC)
- How many images can opencv open?
- List rendering in wechat applet
猜你喜欢

【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。

DAP data scheduling function improvement description
![[team learning] SQL programming language notes - task04](/img/3e/c75f5b4610c6b6700567fe75cd2339.png)
[team learning] SQL programming language notes - task04

2D 照片变身 3D 模型,来看英伟达的 AI 新“魔法”!

Crawler grabs the idea of reading on wechat

1280_C语言求两个无符号整形的平均值

讲座记录《惯性导航的新应用——惯性测量》
![[harmony OS] [arkui] ETS development graphics and animation drawing](/img/9d/0ac2b3d8bcdcd610767df930e2fa4e.png)
[harmony OS] [arkui] ETS development graphics and animation drawing

opencv 红色区域在哪里?

Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.
随机推荐
9 necessary soft skills for program ape career development
Monitoring pancakeswap new token
PHP代码审计1—PHP.ini的那些事
opencv是开源的吗?
The art of writing simple code
Teach you how to install win11 system in winpe
JS tool function, self encapsulating a throttling function
ZABBIX installation pit avoidance Guide
Cesium loading display thermal diagram
Cesium 加载显示热力图
Lecture record: new application of inertial navigation - inertial measurement
How to install opencv? Opencv download installation tutorial
《悉达多》:一生之书,可以时常反刍
How much do you know about the use value of WMS warehouse management system
1280_C语言求两个无符号整形的平均值
虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
On the self-cultivation of an excellent red team member
Numpy NP tips: squeeze and other processing of numpy arrays
地方/園區產業規劃之 “ 如何進行產業定比特 ”
Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning