Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Best Time to Buy and Sell Stock leetcode solution

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int pof=INT_MAX;
        int ans=0;
        for(int i=0;i<prices.size();i++)
        {
            if(prices[i]<pof)
            {
                pof=prices[i];
            }else if(prices[i]-pof>ans)
            {
                ans=prices[i]-pof;
            }
        }
        return ans;
    }
};
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #Best #Time #Buy #Sell #Stock #leetcode #solution
ADD COMMENT
Topic
Name
4+2 =