ToB企服应用市场:ToB评测及商务社交产业平台

标题: Golang | Leetcode Golang题解之第309题交易股票的最佳时机含冷冻期 [打印本页]

作者: 种地    时间: 2024-8-2 20:03
标题: Golang | Leetcode Golang题解之第309题交易股票的最佳时机含冷冻期
题目:

题解:
  1. func maxProfit(prices []int) int {
  2.     if len(prices) == 0 {
  3.         return 0
  4.     }
  5.     n := len(prices)
  6.     f0, f1, f2 := -prices[0], 0, 0
  7.     for i := 1; i < n; i++ {
  8.         newf0 := max(f0, f2 - prices[i])
  9.         newf1 := f0 + prices[i]
  10.         newf2 := max(f1, f2)
  11.         f0, f1, f2 = newf0, newf1, newf2
  12.     }
  13.     return max(f1, f2)
  14. }
  15. func max(x, y int) int {
  16.     if x > y {
  17.         return x
  18.     }
  19.     return y
  20. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4