[LeetCode] 256. Paint House_Easy tag: Dynamic Programming
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x 3
cost matrix. For example, costs[0][0]
is the cost of painting house 0 with color red; costs[1][2]
is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.
Note:
All costs are positive integers.
Example:
Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
Minimum cost: 2 + 5 + 3 = 10.
思路为DP, 关系式为
#A[i][0] += min(A[i-1][1:])
#A[i][1] += min(A[i-1][0], A[i-1][2])
#A[i][2] += min(A[i-1][:2])
Code T; O(n) S: O(n) 可以利用inplace或者rolling array降为O(1)
class Solution:
def minCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
##Solution
#A[i][0] += min(A[i-1][1:])
#A[i][1] += min(A[i-1][0], A[i-1][2])
#A[i][2] += min(A[i-1][:2]) if not costs: return 0
A, n = costs, len(costs)
for i in range(1,n):
A[i][0] += min(A[i-1][1:])
A[i][1] += min(A[i-1][0], A[i-1][2])
A[i][2] += min(A[i-1][:2])
return min(A[-1])
[LeetCode] 256. Paint House_Easy tag: Dynamic Programming的更多相关文章
- [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- liunx trac 插件使用之GanttCalendarPlugin
http://trac-hacks.org/wiki/GanttCalendarPlugin官网上的说明很清楚,处理做几点提示,以做记录. 1.我的Trac版本是1.0.1 我使用了'B' Metho ...
- 【Spring源码深度解析学习系列】注册解析的BeanDefinition(五)
对于配置文件,解析和装饰完成之后,对于得到的beanDefinition已经可以满足后续的使用要求了,还剩下注册,也就是processBeanDefinition函数中的BeanDefinitionR ...
- 【大数据系列】hadoop集群的配置
一.hadoop的配置文件分类 1.只读类型的默认文件 core-default.xml hdfs-default.xml mapred-default.xml mapred-que ...
- makefile高级应用
https://www.zybuluo.com/lishuhuakai/note/206938 make是Linux下的一款程序自动维护工具,配合makefile的使用,就能够根据程序中模块的修改情况 ...
- explain 和 desc 详解
MySQL性能分析 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selecttype table ty ...
- ELK系列五:Logstash输出到Elasticsearch和redis
1.Logstash与Redis的读写 1.1 Logstash 写入Redis 看完Logstash的输入,想必大家都清楚了Logstash的基本用法,那就是写配置文件. output{ { red ...
- 源码包安装Python3.6
1,安装Python3.6的依赖包 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel r ...
- 自定义tarBar
使用tarBar大多数情况在我们都是默认的tarBarButton尺寸和位置但是如果我们想,希望像新浪微博那样的tarBar,就需要自定义了. 1.本质上其实就是通过我们的主控制器中以KVC的方式重新 ...
- 如何搭建SoC项目的基本Testbench【zz】
原文地址:http://bbs.eetop.cn/thread-442797-1-8.html 写这个文档的目的是让大家对搭建SoC项目的Testbench有一个比较清晰的认识,可以根据这个文档来一步 ...
- MySQLCouldn't find MySQL manager
mysql出现MySQLCouldn't find MySQL manager错误的解决办法 最近我的一个linux服务器上的网站全部无法打开.....访问网站没有提示.....只出现该页无法显示的错 ...