class Solution { public: int maxProfit(vector<int>& prices) { int minP=INT_MAX; int maxP=0; for(int i=0;i<prices.size();i++){ minP=min(prices[i],minP); maxP=max(prices[i]-minP,maxP); } return maxP; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter