美文网首页
买卖股票的最佳时机

买卖股票的最佳时机

作者: 7赢月 | 来源:发表于2020-05-05 15:45 被阅读0次

题目描述

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/


package main

func maxProfit(prices []int) int {
    if len(prices) == 0 {
        return 0
    }
    if len(prices) == 1 {
        return 0
    }
    // min 之前的最低价格
    // max 每天卖出的价格
    var min, max = prices[0], 0
    for _, v := range prices {
        if v < min {
            min = v
        }
        if v-min > max {
            max = v - min
        }
    }
    return max
}

思路

这种方法很简单,每天都判断是否卖出。

相关文章

网友评论

      本文标题:买卖股票的最佳时机

      本文链接:https://www.haomeiwen.com/subject/rzbmghtx.html