1. 程式人生 > >【leetcode】python演算法題庫——簡單難度【3】

【leetcode】python演算法題庫——簡單難度【3】

118. 楊輝三角

給定一個非負整數 numRows,生成楊輝三角的前 numRows 行。

在楊輝三角中,每個數是它左上方和右上方的數的和。

示例:

輸入: 5
輸出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

程式碼:

class Solution:
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        res = []
        for i in range(numRows):
            temp = [1] * (i + 1)
            res.append(temp)
            for j in range(1, i):
                res[i][j] = res[i - 1][j - 1] + res[i - 1][j]
        return res

119. 楊輝三角 II

給定一個非負索引 k,其中 k ≤ 33,返回楊輝三角的第 行。

在楊輝三角中,每個數是它左上方和右上方的數的和。

示例:

輸入: 3
輸出: [1,3,3,1]

進階:

你可以優化你的演算法到 O(k) 空間複雜度嗎?

程式碼:

class Solution:
    # 遞迴法
    def getRow(self, rowIndex):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        # if rowIndex == 0:
        #     return []
        if rowIndex == 0:
            return [1]
        if rowIndex == 1:
            return [1,1]
        old_row = self.getRow(rowIndex - 1)
        new_row = [1] * (rowIndex + 1)
        for i in range(1, rowIndex):
            new_row[i] = old_row[i - 1] + old_row[i]
        return new_row

    # 動態更新
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        #if rowIndex==0:return [1]
        ans=[1 for i in range(rowIndex+1)]
        for i in range(1,rowIndex+1):
            for j in range(i-1,0,-1):
                ans[j]=ans[j-1]+ans[j]
        return ans

    # 迴圈o(n^2)複雜度
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        res = []
        for i in range(rowIndex + 1):
            temp = [1] * (i + 1)
            res.append(temp)
            for j in range(1, i):
                 res[i][j] = res[i-1][j-1] + res[i-1][j]
        return res[rowIndex]

121. 買賣股票的最佳時機

給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。

如果你最多隻允許完成一筆交易(即買入和賣出一支股票),設計一個演算法來計算你所能獲取的最大利潤。

注意你不能在買入股票前賣出股票。

示例 1:

輸入: [7,1,5,3,6,4]
輸出: 5
解釋: 在第 2 天(股票價格 = 1)的時候買入,在第 5 天(股票價格 = 6)的時候賣出,最大利潤 = 6-1 = 5 。
     注意利潤不能是 7-1 = 6, 因為賣出價格需要大於買入價格。

示例 2:

輸入: [7,6,4,3,1]
輸出:
0 解釋: 在這種情況下, 沒有交易完成, 所以最大利潤為 0。

程式碼:

class Solution:
    # 動態規劃
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) < 2:
            return 0
        min_price = prices[0]
        max_profit = 0
        for price in prices:
            max_profit = max(price-min_price, max_profit)
            min_price = min(min_price, price)
        return max_profit

122. 買賣股票的最佳時機 II

給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。

設計一個演算法來計算你所能獲取的最大利潤。你可以儘可能地完成更多的交易(多次買賣一支股票)。

注意:你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。

示例 1:

輸入: [7,1,5,3,6,4]
輸出: 7
解釋: 在第 2 天(股票價格 = 1)的時候買入,在第 3 天(股票價格 = 5)的時候賣出, 這筆交易所能獲得利潤 = 5-1 = 4 。
     隨後,在第 4 天(股票價格 = 3)的時候買入,在第 5 天(股票價格 = 6)的時候賣出, 這筆交易所能獲得利潤 = 6-3 = 3 。

示例 2:

輸入: [1,2,3,4,5]
輸出: 4
解釋: 在第 1 天(股票價格 = 1)的時候買入,在第 5 天 (股票價格 = 5)的時候賣出, 這筆交易所能獲得利潤 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接連購買股票,之後再將它們賣出。
     因為這樣屬於同時參與了多筆交易,你必須在再次購買前出售掉之前的股票。

示例 3:

輸入: [7,6,4,3,1]
輸出: 0
解釋: 在這種情況下, 沒有交易完成, 所以最大利潤為 0。

程式碼:

class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) < 2:
            return 0
        min_price = prices[0]
        max_profit = 0
        for price in prices:
            if price > min_price:
                max_profit += price - min_price
                min_price = price
            else:
                min_price = min(min_price, price)
        return max_profit
     
    def maxProfit2(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        profit = 0
        for i in range(1,len(prices)):
            if prices[i] > prices[i-1]:
                profit += prices[i]-prices[i-1]
        return profit