menu Lyanのブログ
Best Time to Buy and Sell Stock
161 浏览 | 2020-04-26 | 分类:动态规划,数据结构与算法 | 标签:

Best Time to Buy and Sell Stock II

Summary

Say you have an array prices for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Approuch 1

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        totalProfit = 0
        valleyPrice = float('inf')
        for pri in prices:
            if pri < valleyPrice:
                valleyPrice = pri
            elif pri > valleyPrice:
                totalProfit += pri - valleyPrice
                valleyPrice = pri
        return totalProfit

Approuch 2

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        maxProfit = 0
        d = 1
        while d < len(prices):
            if prices[d] > prices[d-1]:
                maxProfit += prices[d] - prices[d-1]
            d += 1
        return maxProfit

Best Time to Buy and Sell Stock III

Summary

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Approuch 1

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        dp0, dp1 = [0] * 3, [0] * 3
        dp1[0], dp1[1] = -prices[0], -prices[0]
        for d in range(1, len(prices)):
            dp0[2] = max(dp0[2], dp1[1] + prices[d])
            dp1[1] = max(dp1[1], dp0[1] - prices[d])
            dp0[1] = max(dp0[1], dp1[0] + prices[d])
            dp1[0] = max(dp1[0], -prices[d])
        return max(dp0)

Best Time to Buy and Sell Stock IV

Summary

Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Approuch 1

class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if not prices:
            return 0
        if k > len(prices)/2:
            profit = 0
            for d in range(1, len(prices)):
                profit += max(0, prices[d] - prices[d-1])
            return profit
        dp0 = [0] * (k+1)
        dp1 = [-prices[0]] * (k + 1)
        for d in range(1, len(prices)):
            dp1[0] = max(dp1[0], -prices[d])
            for n in range(1, k+1):
                dp1[n] = max(dp1[n], dp0[n] - prices[d])
                dp0[n] = max(dp0[n], dp1[n-1] + prices[d])
        return max(dp0)

Best Time to Buy and Sell Stock with Cooldown

Summary

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Approuch 1

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)
        if n < 2:
            return 0
        dp0 = [0] * n
        dp0[0], dp1 = 0, -prices[0]
        dp0[1], dp1 = max(0, dp1 + prices[1]), max(dp1, -prices[1])
        for d in range(2, n):
            dp0[d] = max(dp0[d-1], dp1 + prices[d])
            dp1 = max(dp1, dp0[d-2] - prices[d])
        return dp0[-1]

Best Time to Buy and Sell Stock with Transaction Fee

Summary

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

Approuch 1

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        dp0, dp1 = 0, -prices[0]
        for d in range(1, len(prices)):
            pre_dp0 = dp0
            dp0 = max(dp0, dp1 + prices[d] - fee)
            dp1 = max(dp1, pre_dp0 - prices[d])
        return dp0
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

全部评论

info 评论功能已经关闭了呐!