LeetCode 动态规划Climbing Stairs/House Robber/Decode Ways

题目1:

Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

思路1:

动态规划思路,爬第n个楼梯有F(n) = f(n-1) + f(n-2),因为一次要么爬两级要么一级,此时只和上面两个两个状态有关

反过来:初始状态1层楼梯有1种,2层有2种,第三层则有关系:f(3) = f(1) + f(2), 依次往后退

解答1:

class Solution:
    def climbStairs(self, n: int) -> int:
        if n <= 1:
            return 1
        if n == 2:
            return 2
        s1 = 1
        s2 = 2
        for i in range(2, n):
            temp = s1 + s2
            s1 = s2
            s2 = temp
        return temp

题目2 :

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.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
             Total amount you can rob = 2 + 9 + 1 = 12.

思路2:

同上,抢第一个房子有两个结果,抢获得相应钱,不抢,则可以选择抢剩下n-2个房子获得的最多的钱,

此时初始状态为抢f1,1 = M1 或者 不抢f1,0 = 0,第二个房子抢:f2,1 = M2 + f1,0, 不抢 f2,0 = max(f1,1, f1,0),以此类推

抢fn,1 = Mn + fn-1,0,    不抢fn,0 = max(fn-1,1, fn-1,0)

其中:Mn代表第n个房子的钱;fn,1 代表抢第n个房子; fn,0 代表不抢第n个房子

代码2:

class Solution:
    def rob(self, nums: List[int]) -> int:
        if not nums: 
            return 0
        A = nums[0]
        B = 0
        for i in range(1, len(nums)):
            M = B
            B = max(A, B)
            A = M + nums[i]
        return max(A,B)

题目3:

Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: "226"
Output: 3
Exp;lanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

思路3:

依然是动态规划,但是要分情况,分条件:

假设所有的字符之间都是合法的,且两两之间也都可以组成一个字母,此时,转移矩阵为:F(n) = f(n-1) + f(n-2),

初始状态为:l0 = 1, l1=1;

如果连续两个0,则返回0;

如果其中与前一个不能组成一个字母,且不是0,则转移方法为F(n) = f(n-1)

如果其中与前一个不能组成一个字母,或者是0,则转移方法为F(n) = f(n-2)

 

代码3:

class Solution:
    def numDecodings(self, s: str) -> int:
        if s == "" or s[0] == "0":
            return 0
        if len(s) == 1:
            return 1
        l0 = 1
        l1 = 1
        l = 1
        for i in range(1, len(s)):
            if int(s[i-1:i+1])==0 or ((int(s[i-1:i+1])>26 and s[i]=='0')):
                return 0
            elif int(s[i])==0 :
                l = l0
            elif int(s[i-1:i+1])>10 and int(s[i-1:i+1])<=26:
                l = l0 + l1
            elif int(s[i-1:i+1])>26 and int(s[i-1:i+1])<10:
                l = l1
            l0 = l1
            l1 = l     
            
        return l

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值