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. 【Drools-开源业务规则引擎】入门实例(含源码)

    该实例转自:http://blog.csdn.net/quzishen/article/details/6163012 便于理解的应用实例1: 现在我们模拟一个应用场景:网站伴随业务产生而进行的积分发 ...

  2. liunx trac 安装记录

    1,下载地址 http://trac.edgewall.org/   2.安装 apache,python, mysql  3,安装trac (我的是0.12)  tar -zxvf  你下载的安装包 ...

  3. jQuery事件处理(五)

    对原生js不熟悉看jQuery会困难很多.后续需要更多的关注下原生js jQuery封装之后的事件触发,其中一个分支(处理普通事件)是通过:elem.addEventListener( type, e ...

  4. Android 缓存策略demo

    packageinstaller\permission\model\PermissionApps.java /** * Class used to reduce the number of calls ...

  5. WinDbg远程调试unable to initialize target machine information win32 error 0n87

    Debugging Target:Windows XP SP3 32-bit Debugging Host:Windows Server 2012 64-bit 当附加到目标服务器某个进程后,WinD ...

  6. 完美解决Android SDK Manager无法更新

    由于国内的各种屏蔽现在Android SDK Manager出现无法更新或更新太慢,如下方法可完美解决此问题 1. 打开..\Android\sdk\SDK Manager.exe  2.

  7. mysql概要(二)类型(数值型,字符型,时间类型

    1.mysql数值型范围 tinyint可选属性 tinyint(N) unsigned zerofill N:表示显示长度,与zerofill配合使用,即长度不够用0填充,并且自动变成无符号的数,N ...

  8. Linux下识别所有Android设备的方法

    修改/etc/udev/rules.d/51-android.rules文件. 方法一: 参考Google文档 SUBSYSTEM=="usb", ATTR{idVendor}== ...

  9. hadoop HA架构安装部署(QJM HA)

    ###################HDFS High Availability Using the Quorum Journal Manager########################## ...

  10. queue hardware os

    Computer Science An Overview 11th Edition Queues are often used as the underlying structure of a buf ...