当前位置:网站首页>[Jianzhi offer II 091. painting the house]
[Jianzhi offer II 091. painting the house]
2022-06-25 16:49:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
If there is a row of houses , common n
individual , Every house can be painted red 、 One of the three colors blue or green , You need to paint all the houses and make the two adjacent houses not the same color .
Of course , Because the prices of different colors of paint on the market are different , So the cost of painting the house in different colors is different . The cost of painting each house in a different color is one n x 3
Positive integer matrix of costs
To represent the .
for example ,costs[0][0]
It means the first one 0 The cost of painting the house red ;costs[1][2]
It means the first one 1 The cost of painting the house green , And so on .
Please calculate the minimum cost of painting all the houses .
Example 1:
Input : costs = [[17,2,17],[16,16,5],[14,3,19]]
Output : 10
explain : take 0 No. 1 house painted blue ,1 No. 1 house painted green ,2 No. 1 house painted blue .
Minimum cost : 2 + 5 + 3 = 10.
Example 2:
Input : costs = [[7,6,2]]
Output : 2
Tips :
- costs.length == n
- costs[i].length == 3
- 1 <= n <= 100
- 1 <= costs[i][j] <= 20
Method : Dynamic programming
Code :
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
int n = costs.size();
vector<int> dp(3);
for (int j = 0; j < 3; j++) {
dp[j] = costs[0][j];
}
for (int i = 1; i < n; i++) {
vector<int> dpNew(3);
for (int j = 0; j < 3; j++) {
dpNew[j] = min(dp[(j + 1) % 3], dp[(j + 2) % 3]) + costs[i][j];
}
dp = dpNew;
}
return *min_element(dp.begin(), dp.end());
}
};
Execution time :4 ms, In all C++ Defeated in submission 96.45% Users of
Memory consumption :9.5 MB, In all C++ Defeated in submission 56.50% Users of
author:LeetCode-Solution
边栏推荐
- Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文
- 【蓝桥杯集训100题】scratch指令移动 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第14题
- Prototype chain analysis
- Understanding of reflection part
- 2022-06-17 advanced network engineering (IX) is-is- principle, NSAP, net, area division, network type, and overhead value
- 【精通高并发】深入理解汇编语言基础
- 心樓:華為運動健康的七年築造之旅
- Apijson simple to use
- The problem of missing precision of kettle table input components
- 完美洗牌问题
猜你喜欢
随机推荐
Ad domain login authentication
Day_ eleven
什么是骨干网
2022-06-17 网工进阶(十)IS-IS-通用报头、邻接关系的建立、IIH报文、DIS与伪节点
从TiDB上线阿里云的背后,如何看待云数据库的变革趋势
六大专题全方位优化,阿里巴巴性能优化小册终开源,带你直抵性能极致
A TDD example
Prototype chain analysis
The first day of reading mysql45
Cache architecture scheme of ten million level shopping cart system
【精通高并发】深入理解C语言基础与汇编下的C语言
pychrm的这些配置,你都知道吗?
Notes: lbcf: a Large Scale budget Constrained causal Forest Algorithm
3.条件概率与独立性
Swift responsive programming
论文笔记:Generalized Random Forests
2022-06-17 advanced network engineering (x) is-is-general header, establishment of adjacency relationship, IIH message, DIS and pseudo node
Structure de la mémoire JVM
Uniapp to preview pictures (single / multiple)
JVM memory structure