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的更多相关文章

  1. [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 ...

  2. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming

    基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Windows平台下PHP7添加Sqlserver扩展

    1.7.0.x 7.0.x的扩展下载地址: Microsoft Drivers for PHP for SQL Server  https://www.microsoft.com/en-us/down ...

  2. 重建索引:ALTER INDEX..REBUILD ONLINE vs ALTER INDEX..REBUILD

    什么时候需要重建索引 1. 删除的空间没有重用,导致 索引出现碎片 2. 删除大量的表数据后,空间没有重用,导致 索引"虚高" 3.索引的 clustering_facto 和表不 ...

  3. java.lang.NoClassDefFoundError: Could not initialize class xxx 原因

    一.问题及原因 程序里有个工具类,主要是调用它的静态方法来发送mq. 调用场景如下: 结果这两天报了个错: java.lang.NoClassDefFoundError: Could not init ...

  4. github的README.md文件格式

    一.在线编辑器:stackedit 网址:https://stackedit.io/ README.md是使用Markdown语法.基本规则如下: Markdown 语法速查表 1 标题与文字格式 标 ...

  5. Unity3D笔记 愤怒的小鸟<三> 实现Play界面2

    前言:在Play页面中给Play页面添加一个“开始游戏”和“退出游戏”按钮顺便再来一个背景音乐 添加按钮可以是GUI.Button(),也可以是GUILayout.Button():给图片添加按钮可以 ...

  6. git如何回滚当前修改的内容?

    git如何回滚当前修改的内容? 1.打开git gui,在工具栏上点击 commit ,选择 Revert Changes,  这里可以回滚单个文件: 2.一键回滚所有修改: 打开git gui,在工 ...

  7. html css的内联样式 内部样式表 外部样式表的优先级

    http://www.w3school.com.cn/html/html_css.asp 这三种样式是有优先级的,记住他们的优先级:内联式 > 嵌入式 > 外部式,但是嵌入式>外部式 ...

  8. tomcat启动报错:serializer.jar (系统找不到指定的文件。)

    下载最新对应版本的tomcat.移除之前的tomcat.删除原本全部tomcat的目录. 疑似tomcat的lib包被动过.

  9. iOS - viewDidLoad, viewWillDisappear, viewWillAppear区别及加载顺序

    viewWillAppear: Called when the view is about to made visible. Default does nothing视图即将可见时调用.默认情况下不执 ...

  10. R子集subset

    > x<-c(6,1,2,3,NA,12) > x[x>5]    #x[5]是未知的,因此其值是否大于5也是未知的 [1]  6 NA 12 > subset(x,x& ...