leetcode-买卖股票的最佳时机④*
生活随笔
收集整理的這篇文章主要介紹了
leetcode-买卖股票的最佳时机④*
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述:
方法一:
class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: if len(prices) <= 1: return 0 if (k < len(prices) // 2) : dp = [[-prices[0], 0] for i in range(k+1)] for price in prices[1:]: for i in range(1, k+1): dp[i] = [max(dp[i][0], dp[i-1][1]-price), max(dp[i][1], dp[i][0]+price)] return dp[k][1] else: dp = [-prices[0], 0] for price in prices[1:]: dp = [max(dp[0], dp[1]-price), max(dp[1], dp[0]+price)] return dp[1]?
轉載于:https://www.cnblogs.com/oldby/p/11190721.html
總結
以上是生活随笔為你收集整理的leetcode-买卖股票的最佳时机④*的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爬虫---Beautiful Soup
- 下一篇: 抽一个读者,送她一台MacBook!