leetcode-剑指63-OK

// language c
// 剑指63
// https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof/
// 同主站121
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/



int maxProfit(int* prices, int pricesSize){
    if(pricesSize <=1)
        return 0;
    int lowest=prices[0];
    int ans = 0;
    // 每次遇上新低 就替换lowest
    // 每次遇上涨价 就check是否能替换ans
    for(int i =1; i<pricesSize;i++){
        if(prices[i] <lowest)
            lowest = prices[i];
        else if(prices[i]-lowest > ans)
                ans = prices[i]-lowest;
    }
    return ans;
}
posted @ 2021-01-27 14:41  RougeBW  阅读(34)  评论(0编辑  收藏  举报