Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 22, 2018 05:41

Revisions

  1. kanrourou created this gist Dec 22, 2018.
    14 changes: 14 additions & 0 deletions Best Time to Buy and Sell Stock.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    class Solution {
    public:
    int maxProfit(vector<int>& prices) {
    if(prices.empty())return 0;
    int len = prices.size(), globalMax = 0, minPrice = prices[0];
    for(int i = 1; i < len; ++i)
    {
    int localMax = prices[i] - minPrice;
    globalMax = max(globalMax, localMax);
    minPrice = min(minPrice, prices[i]);
    }
    return globalMax;
    }
    };