当前位置:网站首页>Leetcode daily question - Sword finger offer II 091 Paint the house
Leetcode daily question - Sword finger offer II 091 Paint the house
2022-06-28 20:40:00 【Did HYK write the algorithm today】
List of articles
subject
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
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
Ideas
Typical dynamic programming problems , You can combine the costs Array as dp Array , First column 、 Second column 、 The meaning of the third column is the cost of painting the wall in this column , The state transition equation is this column plus one row and two other columns , Finally back to dp The minimum value of the last row of the array .
Answer key
class Solution:
def minCost(self, costs: List[List[int]]) -> int:
n = len(costs) - 1
for i in range(1, len(costs)):
# State transition equation
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])
边栏推荐
- 理解整个网络模型的构建
- Shell reads the value of the JSON file
- 实型数运算
- 如何使用 DataAnt 监控 Apache APISIX
- RT-Thread线程同步与线程通信
- 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
- Explanation of memory dump triggered by software watchdog and anr
- 如何做好客户成功的底层设计|ToB大师课
- 如何添加 logs来debug ANR 问题
- Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
猜你喜欢

ThreadLocal原理

RT-Thread线程同步与线程通信

Lucene构建索引的原理及源代码分析

【毕业季·进击的技术er】努力只能及格,拼命才能优秀!

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation

Bitbucket failed to pull the warehouse Using SSH

ThreadLocal principle

Flatten of cnn-lstm

Visualization of neural network structure in different frames

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
随机推荐
How to understand the usability of cloud native databases?
【学习笔记】因子分析
【学习笔记】主成分分析法介绍
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
Ref attribute, props configuration, mixin mixing, plug-in, scoped style
ThreadLocal principle
【Try to Hack】Cobalt Strike(一)
应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
Input and output character data
Win 10 create a gin framework project
【读书会第13期】视频文件的封装格式
Anr analysis - question 1
如何添加 logs来debug ANR 问题
Comparisonchain file name sort
Are you still paying for your thesis? Come and join me
LeetCode每日一题——30. 串联所有单词的子串
Flatten of cnn-lstm
Bluecmsv1.6 code audit
Explanation of memory dump triggered by software watchdog and anr
Automatic operation and maintenance platform based on Apache APIs