O(n) time O(1) space java solution with 5-line code


  • 1
    X
    public class Solution {
        public int maxProfit(int[] prices) {
            int min = Integer.MAX_VALUE, mp = 0;
            for(int i: prices){
                min = Math.min(min,i);
                mp = Math.max(mp, i-min);
            }
            return mp;
        }
    }

  • 0
    C

    Why did you tag this 'dynamic programming'?


  • 0
    X

    Since min & mp is determined by their value in previous status, I tag this as a DP solution.


Log in to reply
 

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