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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章