Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 22, 2018 05:41
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;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment