leetcode stock issue

leetcode stock issue

Reference: labuladong

(Leetcode-121) Given an array, which i-th element is a given stock i-day prices. If you are only allowed up to complete a transaction (ie buying and selling a stock), to design an algorithm to compute the maximum profit you can get. Note that you can not sell the stock before buying stocks.

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        minp = float('inf')
        maxpr = float('-inf')
        if not prices:
            return 0
        for i,v in enumerate(prices):
            minp = min(minp,v)
            maxpr = max(v-minp,maxpr)
        return maxpr
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        dp = [[0,0] for _ in range(1+len(prices))]
        dp[0][1]=-prices[0]
        for i in range(1,len(prices)):
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i])
            dp[i][1]=max(dp[i-1][1],-prices[i])
        return dp[len(prices)-1][0]

(leetcode-122) Given an array, which i-th element is a given stock i-day prices. Design an algorithm to compute the maximum profit you can get. You can close more deals (buy and sell a stock) as much as possible. Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).
A thought: as long as the high price the next day, I bought

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        sumr = 0
        if len(prices) == 0:
            return 0
        for i in range(1,len(prices)):
            tmp = prices[i] - prices[i-1]
            if tmp > 0:
                sumr += tmp
        return sumr

Thinking two: in both cases, cash represents cash I have: 1) hold the original; 2) the stock sold, earning the current stock of money; hold the stock holdings represent: 1) hold the original stock; 2 ) use cash to buy stocks;

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        cash = 0
        hold = -prices[0]
        n = len(prices)
        for i in range(1,n):
            cash = max(cash,hold+prices[i])
            hold = max(hold,cash-prices[i])
        return cash

(leetcode-714) Given an array of integers prices, which represent the i-th element of the i-day stock price; non-negative integer fee represents the cost of trading stocks procedures. You can complete the transaction unlimited number of times, but you need to pay the fee for each transaction. If you have purchased a stock, sell it before you can no longer continue to buy stocks. Returns the maximum of profit.
Leetcode 221 with the second approach, as much to pay fees;

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        ca = 0
        hd = -prices[0]
        for i in range(1,len(prices)):
            ca = max(ca,hd+prices[i]-fee)
            hd = max(hd,ca-prices[i])
        return ca

(leetcode-309) Given an array of integers, which represents the i-th element of the i-day stock price. Design an algorithm to calculate the maximum profit. In the following constraints, you can complete more transactions (buy and sell a stock) as much as possible: You can not simultaneously involved in multiple transactions (you have to sell the stock before the fall before buying again). After selling the stock, you can not buy the stock the next day (ie freeze to one day).
Thinking: this time to buy stocks hold when the cash is not i-1, i-2 instead of

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        cash = 0
        hold = -prices[0]
        cash_pre = 0
        for i in range(1,len(prices)):
            tmp = cash
            cash = max(cash,hold+prices[i])
            hold = max(hold,cash_pre-prices[i])
            cash_pre = tmp
        return cash

(Leetcode-123) Given an array, which i-th element is the price of a given stock i-th day. Design an algorithm to compute the maximum profit you can get. You can complete up to two transactions. Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        cash_1 = 0
        cash_2 = 0
        hold_1 = -prices[0]
        hold_2 = -prices[0]
        for i in range(1,len(prices)):
            cash_1 = max(cash_1,hold_1+prices[i]) // 把第二次的股票卖了
            hold_1 = max(hold_1,cash_2-prices[i]) // 拿第一次的钱买第二次的股票
            cash_2 = max(cash_2,hold_2+prices[i]) // 把第一次持有的股票卖了
            hold_2 = max(hold_2,-prices[i]) // 第一次买股票
        return cash_1

(Leetcode-188) Given an array, which i-th element is the price of a given stock i-th day. Design an algorithm to compute the maximum profit you can get. You can complete most k transactions. Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).

class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if not prices:
            return 0
        n = len(prices)
        if k > n:
            sumr = 0
            if len(prices) == 0:
                return 0
            for i in range(1,len(prices)):
                tmp = prices[i] - prices[i-1]
                if tmp > 0:
                    sumr += tmp
            return sumr

        g = [0] * (k + 1)
        l = [0] * (k + 1)
        for i in range(n - 1):
            diff = prices[i + 1] - prices[i]
            for j in range(k, 0, -1):
                l[j] = max(g[j - 1] + max(diff, 0), l[j] + diff)
                g[j] = max(l[j], g[j])
        return g[-1]

Guess you like

Origin www.cnblogs.com/curtisxiao/p/11318648.html