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. java框架---->lucene的使用(一)

    Lucene是一个全文检索的框架,apache组织提供了一个用Java实现的全文搜索引擎的开源项目.这里我们对apache的lucene的框架做一个简单的介绍.心甘情愿这四个字,透着一股卑微,但也有藏 ...

  2. 【大数据系列】hadoop集群设置官方文档翻译

    Hadoop Cluster Setup Purpose Prerequisites Installation Configuring Hadoop in Non-Secure Mode Config ...

  3. v-bind小demo

    啊哈哈,小颖好久没有更新博客啦,大家有没有想我呀,嘻嘻,自恋一把,

  4. nginx+memcache实现页面缓存应用

    一.前言 nginx的memcached_module模块可以直接从memcached服务器中读取内容后输出,后续的请求不再经过应用程序处理,如php-fpm.django,大大的提升动态页面的速度. ...

  5. nginx作为下载文件服务器

    1.前言 当我们希望分享自己的文件时,有多种方式,局域网可以采用共享,rtx传输,qq传输,发送到邮箱,直接u盘拷贝等等.但最简单的就是开启本地服务器,其他电脑通过网页的方式直接下载,这里介绍使用ng ...

  6. Jmeter TCP取样器配置及发送图解

    最近在通过Jmeter测试TCP发送请求时,遇到相关问题,现记录 查看管方文档,TCP发送有三种启用方式: TCPClientImpl:文本数据,默认为这种 BinaryTCPClientImpl:传 ...

  7. 初始react

    很久就期待学习react了,惰性,一直都没有去翻阅react的资料,最近抽空,简单的了解了一下react,先记录一下,后续慢慢的学习. 一.ReactJS简介 React 起源于 Facebook 的 ...

  8. npm publish 发布

    前言 我们npm publish发布的时候,一定是本地文件发布到远程仓库,并且登录到http://registry.npmjs.org(即npm adduser或npmlogin)之后,才可以进行发布 ...

  9. Spark2 探索性数据统计分析

    data数据源,请参考我的博客http://www.cnblogs.com/wwxbi/p/6063613.html import org.apache.Spark.sql.DataFrameStat ...

  10. UI自动化--PageObjects(页面对象)

    核心的核心:减少了重复代码的数量,减少变更涉及面:做到如果UI发生更改,则只需在一个位置应用此修复程序. PageObject:将页面作为一个对象,进行封装,包括元素定位,封装获取各元素.操作的方法: ...