O(1) Space O(N) solution

  • 0
    • list item Question in short is finding i and j, such that i <j and prices[j]-prices[i] is maximum
    int maxProfit(vector<int>& prices) {
            int ans = 0 , minS = prices[0];
            for(int i=1;i<prices.size();i++){
                minS = min(minS,prices[i]);
                ans = max(ans,prices[i]-minS);
            return ans;
        return 0;

Log in to reply

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