C++ clean and concise


  • 0
    T
    /*
        只能一笔交易。
        记录当前出现的最小值minprice
        更新ans=max(ans,prices[i]-minprice)
        更新minprice=min(minprice,prices[i])
    */
    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int minprice = INT_MAX, ans =0;
            for(int i=0;i<prices.size();i++)
            {
                ans=max(ans,prices[i]-minprice);
                minprice=min(minprice,prices[i]);
            }
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.