/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2025年12月17日 星期三

C C++ Py3 DP與OOP解Leetcode 3573 Best Time to Buy and Sell Stock V


C C++ Py3  DP與OOP解Leetcode 3573  Best Time to Buy and Sell Stock V
[Py3 code請進]

[python單迴圈解答最佳股票獲利LeetCode 121 Best Time to Buy and Sell Stock]https://www.youtube.com/watch?v=--vVXnaKPqI
class Data:
    def __init__(self, profit, buy, sell):
        self.profit=profit
        self.buy=buy
        self.sell=sell
class Solution:
    def maximumProfit(self, prices: List[int], k: int) -> int:
        x0=prices[0]
        dp=[Data(0, -x0, x0) for _ in range(k+1)]
        n=len(prices)
        for i in range(1, n):
            x=prices[i]
            for t in range(k, 0, -1):
                cur=dp[t]
                prevP=dp[t-1].profit
                # close transaction t
                cur.profit=max(cur.profit, cur.buy+x, cur.sell-x)
                # open transaction t
                cur.buy=max(cur.buy,  prevP-x)
                cur.sell=max(cur.sell, prevP+x)
        return dp[-1].profit

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章