O(n) time and O(1) space 1ms easy to understand java solution

  • 1

    Sell stock if we get profit and always assign as the previous value.

         int profit =0;int prev = Integer.MAX_VALUE;
         for(int i=0;i<prices.length;i++){
          if(prev < prices[i]){
              profit +=  prices[i]-prev;
          }prev =  prices[i];
         return profit;

Log in to reply

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