198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last, now = 0,0
for i in nums:
last, now= now, max(last+i, now)
return now # redo this later
# n,m = 1,2
# n,m = n+m, n variables on the left side present the original ones
# n==3,m==1

746. Min Cost Climbing Stairs

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

Note:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999]
class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
""" take0,take1 = 0,0
for i in cost:
take0,take1 = take1, min(take0,take1)+i
return min(take0,take1)
     # redo later

LeetCode with Python -> Dynamic Programming的更多相关文章

  1. [LeetCode] questions conclusion_ Dynamic Programming

    Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...

  2. Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...

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

  4. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

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

  6. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

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

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

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

随机推荐

  1. spring中用xml配置构造注入的心得

    spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...

  2. Unity结合Flask实现排行榜功能

    业余做的小游戏,排行榜本来是用PlayerPrefs存储在本地,现在想将数据放在服务器上.因为功能很简单,就选择了小巧玲珑的Flask来实现. 闲话少叙.首先考虑URL的设计.排行榜无非是一堆分数sc ...

  3. java中list强转为map类型

    起因:读取数据库文件的测试用例,测试用例需要存放到一个map中,方便下次调用, 读取的内容返回的内容存放在一个list中,并且数据内容是key=value的形式,最开始使用切片方式,做了很多无用功,后 ...

  4. 五、react中父子组件间如何传值

    1.父组件向子组件传递数据:父组件绑定属性值传给子组件,子组件通过this.props()接受. 2.子组件向父组件传递数据:子组件绑定一个方法,方法中通过this.props.父组件方法名(参数)传 ...

  5. 选中ListBox控件中的全部项

    实现效果: 知识运用: ListBox控件的SelectedItems属性 //获取ListBox控件中被选中数据项的集合 public ListBox.SelectedObjectCollectio ...

  6. 2018.2.6 JS-判断用户浏览器

    JS-判断用户浏览器 在判断用户使用的浏览器是否为PC还是移动设备,有时候项目中需要用到.可在需要的项目中当全局方法来使用. 判断代码 function getMoblieDevice(window) ...

  7. 父子组件通信(vuex的方式)

    转: https://blog.csdn.net/lzh5997/article/details/80407518 父子组件也可以通过vuex的进行来进行中转,其实vuex就类似与一个仓库,父组件把东 ...

  8. python_21_copy

    import copy person=['name',['saving',100]] #3种浅copy方式 p1=copy.copy(person) p2=person[:] p3=list(pers ...

  9. WPF实现ListView大小图标和分组

    XAML: <ListView Grid.Row="3" Name="t_lvw_time" HorizontalAlignment="Stre ...

  10. SOA架构,dubbo,Zookeeper

    1. 分析 由于项目是基于soa的架构,表现层和服务层是不同的工程.所以要实现查询需要两个系统之间进行通信. 如何实现远程通信? 1.Webservice:效率不高基于soap协议.项目中不推荐使用. ...