Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 551 Bytes

8.md

File metadata and controls

22 lines (20 loc) · 551 Bytes

Best Time To Buy and Sell Stocks

LeetCode Link

    int maxProfit(vector<int>& prices) {
        if(prices.size()==0){
            return 0;
        }
        int minPrice=prices[0];
        int maxProfit=0;
        for(int i=1;i<prices.size();i++){
            if(minPrice>prices[i]){
                minPrice=prices[i];
            }
            else{
                maxProfit=max(maxProfit,prices[i]-minPrice);
            }
        }

        return maxProfit;
    }