zoukankan      html  css  js  c++  java
  • LeetCode with Python -> Dynamic Programming

    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
  • 相关阅读:
    考研复试之dp重温
    面试总结
    决策树学习记录
    python机器学习随笔
    大三狗重新复习算法之递推
    第十二届CSP总结
    unity(Exploder插件)研究
    unity学习笔记2
    Unity3d学习笔记(持续更新)。。。
    4.24
  • 原文地址:https://www.cnblogs.com/DianeSoHungry/p/8245024.html
Copyright © 2011-2022 走看看