Paint House I/II

要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost

https://repl.it/Chwe/1 (I)

  • 善用slicing来eliminate list中一点,还有一点好处是不用考虑超越边界了

II:如何从O(nkk)降到O(n*k)? 每次找到上一个房子编号list的的min这个循环如果在每个k都做一遍,肯定是redundant的。其实loop一遍就能找到对所有颜色k需要的min:min和second_min:second_min用于min对应颜色的上一个房子

错误点:

  • 注意不要搞混min/second_min的对象:因为当前颜色的cost是固定的。要min的是上一个房子编号的选择
  • list comprehension: if else的优先级低于+,所以+两种可能之一要把if else加括号
  • second_smallest的更新:如果smallest变了,第一件事是更新second_smallest,没变则比较second_smallest更新:所以是两处,并注意顺序

https://repl.it/Chxo/2 (II)

错误点:

  • 小心k和循环下标混了
  • 1d list就能搞定,因为下一个只依赖于前一个
# 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. # Hide Company Tags LinkedIn
# Hide Tags Dynamic Programming
# Hide Similar Problems (E) House Robber (M) House Robber II (H) Paint House II (E) Paint Fence class Solution(object):
def minCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
prev = [0]*3
for colors in costs:
prev = [colors[i] + min(prev[:i]+prev[i+1:]) for i in xrange(3)] return min(prev) sol = Solution()
assert sol.minCost([[1,2,3],[4,5,6],[7,8,9]])==13
# There are a row of n houses, each house can be painted with one of the k colors. 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 k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

# Note:
# All costs are positive integers. # Follow up:
# Could you solve it in O(nk) runtime? # Hide Company Tags Facebook
# Hide Tags Dynamic Programming
# Hide Similar Problems (M) Product of Array Except Self (H) Sliding Window Maximum (M) Paint House (E) Paint Fence class Solution(object):
def minCostII(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
if not costs: return 0
n,k = len(costs), len(costs[0])
prev = [0]*k for j in xrange(k):
prev[j]=costs[0][j] for i in xrange(1, n):
minVal, secondMin = float("inf"), float("inf")
minIdx= -1
for j in xrange(k):
if prev[j]<minVal:
secondMin = minVal
minVal, minIdx = prev[j], j
elif prev[j]<secondMin:
secondMin = prev[j] prev = [costs[i][j] + (minVal if j!=minIdx else secondMin) for j in xrange(k)] return min(prev) sol = Solution()
assert sol.minCostII([[1,5,3],[2,9,4]])==5
assert sol.minCostII([[1,2,3],[4,5,6],[7,8,9]])==13

边工作边刷题:70天一遍leetcode: day 77的更多相关文章

  1. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  2. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  3. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  4. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  5. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  6. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  7. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  8. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

  9. 边工作边刷题:70天一遍leetcode: day 71-1

    Longest Substring with At Most K Distinct Characters 要点:要搞清楚At Most Two Distinct和Longest Substring W ...

随机推荐

  1. Java、Hibernate(JPA)注解大全

    1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...

  2. CSS层次选择器温故-2

    1.层次选择器 通过HTML的DOM元素间的层次关系获取元素,层次关系包括后代.父子.相邻兄弟和通用兄弟,通过其中某类关系可以方便快捷地选定需要的元素 2.语法 3.兼容性 IE7以及以上版本 4.后 ...

  3. Css文字特效之text-shadow特效

    今天总结一下文字特效text-shadow,如果用好它可以做出各种不一样的效果,下图是我做出的几种效果. 怎么样,看起来很不错吧,下面贴代码. /* css */ p{ width:300px; ma ...

  4. Force.com微信开发系列(七)OAuth2.0网页授权

    OAuth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某一网站上存储的私密资源(如用户个人信息.照片.视频.联系人列表),而无须将用户名和密码提供给第三方应用.本文将详细介绍OA ...

  5. MySQL全文索引应用简明教程

    本文从以下几个方面介绍下MySQL全文索引的基础知识: MySQL全文索引的几个注意事项 全文索引的语法 几种搜索类型的简介 几种搜索类型的实例 全文索引的几个注意事项 搜索必须在类型为fulltex ...

  6. Quartz2D学习笔记

    1.drawRect方法 //1.证明drawRect方法是在viewDidLoad后自动调用的,方便处理View的相关属性 // YQView * view = [[YQView alloc] in ...

  7. iOS开发笔记15:地图坐标转换那些事、block引用循环/weak–strong dance、UICollectionviewLayout及瀑布流、图层混合

    1.地图坐标转换那些事 (1)投影坐标系与地理坐标系 地理坐标系使用三维球面来定义地球上的位置,单位即经纬度.但经纬度无法精确测量距离戒面积,也难以在平面地图戒计算机屏幕上显示数据.通过投影的方式可以 ...

  8. iOS:交换Button中图片与文字的左右位置

    titleEdgeInsets属性和 imageEdgeInsets属性只是在画这个button出来的时候用来调整image和label位置的属性,并不影响button本身的大小.它们只是image和 ...

  9. 阶乘 求n!中质因数的个数

    在n!中末尾有几个0 取决于n!中5的个数(2肯定比5多) 所以遍历从1到n的数,看总共有几个5即可 ..N do j = i; == ) ++ret; j /= ; end end 有个nb的方法: ...

  10. CSS从大图片上截取小图标的操作(转)

    一张图片,用CSS分割成多个小图标. css样式: .icon{ background:url(../images/tabicons.png) no-repeat;width:18px; line-h ...