Easy to understand Java solution with T: O(n) S: O(1)

facebook
Tags: #<Tag:0x00007f181b29b938>

#1
public class Solution {
    // DO NOT MODIFY THE LIST. IT IS READ ONLY
    public int maxProfit(final List<Integer> A) {
        int max = 0;
        int min = (A.isEmpty())?0: A.get(0);
        for(int i = 1; i < A.size(); i++) {
            min = Math.min(A.get(i - 1), min);
            int profit = A.get(i) - min;
            max = Math.max(max, profit);
        }
        return max;
    }
}