stock sell 股票交易类

You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can sell and buy the stock multiple times on the same day, ensuring you never hold more than one share of the stock.

Find and return the maximum profit you can achieve.

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.

Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.

Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.

 Constraints:

  • 1 <= prices.length <= 3 * 104
  • 0 <= prices[i] <= 104
class Solution:
    # def maxProfit(self, prices: List[int]) -> int:
    #     lenp = len(prices)
    #     dp = [[-10000] * (lenp + 1) for _ in range(2)]
    #     dp[0][0] = 0
    #     for i in range(1, lenp + 1):
    #         dp[0][i] = max(dp[0][i - 1], dp[1][i - 1] + prices[i - 1])
    #         dp[1][i] = max(dp[0][i - 1] - prices[i - 1], dp[1][i - 1])
    #     return max(dp[0][-1], dp[1][-1])


    def maxProfit(self, prices: List[int]) -> int:
        @cache
        def dfs(day, hold):
            if day < 0:
                return -1000000 if hold else 0
            if hold:
                return max(dfs(day - 1, 0) - prices[day], dfs(day - 1, 1))
            return max(dfs(day - 1, 0), dfs(day - 1, 1) + prices[day])
        return dfs(len(prices) - 1, 0)

'''
到第i天的时候持有股票: dfs(i, true)
    case1: 第i-1天的时候持有股票股票,没发生变化 dfs(i - 1, true)
    case2: 第i-1天的时候不持有股票,那么i天的时候买入了 dfs(i - 1, false) - prices[i]
到第i天的时候不持有股票:
    case1: 第i-1天的时候持有股票股票,那么i天的时候卖了 dfs(i - 1, true) + prices[i]
    case2: 第i-1天的时候不持有股票,没发生变化  dfs(i - 1, false)

边界条件:
    第0天的时候,持有股票是不可能: dfs(-1, true)=-inf
    第0天的时候,不持有股票:dfs(-1, false)=0

'''

 

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:

  • After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).

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

Example 1:

Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

Example 2:

Input: prices = [1]
Output: 0

Constraints:

  • 1 <= prices.length <= 5000
  • 0 <= prices[i] <= 1000
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        @cache
        def dfs(i, hold):
            if i < 0:
                return -100000 if hold else 0
            if hold:
                return max(dfs(i - 1, True), dfs(i - 2, False) - prices[i])
            return max(dfs(i - 1, True) + prices[i], dfs(i - 1, False))
        return max(dfs(len(prices) - 1, True), dfs(len(prices) - 1, False))

'''
到第i天的时候持有股票: dfs(i, true)
    case1: 第i-1天的时候持有股票股票,没发生变化 dfs(i - 1, true)
    case2: 第i-2天的时候不持有股票,那么i天的时候买入了 dfs(i - 2, false) - prices[i]
    ***为什么是第i-2天不持有???因为卖出后无法在第二天买入

到第i天的时候不持有股票:
    case1: 第i-1天的时候持有股票股票,那么i天的时候卖了 dfs(i - 1, true) + prices[i]
    case2: 第i-1天的时候不持有股票,没发生变化  dfs(i - 1, false)

边界条件:
    第0天的时候,持有股票是不可能: dfs(-1, true)=-inf
    第0天的时候,不持有股票:dfs(-1, false)=0
'''
188. Best Time to Buy and Sell Stock IV
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.

Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times.

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

Example 1:

Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

Constraints:

  • 1 <= k <= 100
  • 1 <= prices.length <= 1000
  • 0 <= prices[i] <= 1000
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        @cache
        def dfs(i, hold, j):
            if j < 0:
                return -inf
            if i < 0:
                return -inf if hold else 0
            if hold:
                return max(dfs(i - 1, 1, j), dfs(i - 1, 0, j - 1) - prices[i])
            else:
                return max(dfs(i - 1, 1, j) + prices[i], dfs(i - 1, 0, j))
        return dfs(len(prices) - 1, 0, k)

'''
到第i天的时候持有股票: dfs(i, true)
    case1: 第i-1天的时候持有股票,没发生变化 dfs(i - 1, true, k)
    case2: 第i-1天的时候不持有股票,那么i天的时候买入了 dfs(i - 1, false, k) - prices[i]

到第i天的时候不持有股票:
    case1: 第i-1天的时候持有股票股票,那么i天的时候卖了 dfs(i - 1, true) + prices[i]
    case2: 第i-1天的时候不持有股票,没发生变化  dfs(i - 1, false)

边界条件:
    如果k小于0,不能买卖股票, return inf
    第0天的时候,持有股票是不可能: dfs(-1, true)=-inf
    第0天的时候,不持有股票:dfs(-1, false)=0

'''

 

You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

Note:

  • You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
  • The transaction fee is only charged once for each stock purchase and sale.

Example 1:

Input: prices = [1,3,2,8,4,9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
- Buying at prices[0] = 1
- Selling at prices[3] = 8
- Buying at prices[4] = 4
- Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Example 2:

Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6 

Constraints:

  • 1 <= prices.length <= 5 * 104
  • 1 <= prices[i] < 5 * 104
  • 0 <= fee < 5 * 104
class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        @cache
        def dfs(i, hold):
            if i < 0:
                return -inf if hold else 0
            if hold:
                return max(dfs(i - 1, 1), dfs(i - 1, 0) - prices[i])
            else:
                return max(dfs(i - 1, 0), dfs(i - 1, 1) + prices[i] - fee)
        return dfs(len(prices) - 1, 0)

'''
在买入或者卖出股票的时候扣除额外的fee

到第i天的时候持有股票: dfs(i, true)
    case1: 第i-1天的时候持有股票,没发生变化 dfs(i - 1, true, k)
    case2: 第i-1天的时候不持有股票,那么i天的时候买入了 dfs(i - 1, false, k) - prices[i]

到第i天的时候不持有股票:
    case1: 第i-1天的时候持有股票股票,那么i天的时候卖了 dfs(i - 1, true) + prices[i] - fee
    case2: 第i-1天的时候不持有股票,没发生变化  dfs(i - 1, false)

边界条件:
    第0天的时候,持有股票是不可能: dfs(-1, true)=-inf
    第0天的时候,不持有股票:dfs(-1, false)=0

'''

 

posted @ 2025-11-30 11:38  xiaoyongyong  阅读(2)  评论(0)    收藏  举报