Java O(1) space solution.


  • 0
    X
    public int maxSubArray(int[] nums) {
            int max = nums[0];
            int maxSoFar = max;
            for(int i=1;i<nums.length;i++){
                max = max < 0 ? nums[i] : nums[i]+max;
                maxSoFar = Math.max(maxSoFar, max);
            }
            return maxSoFar;
        }
    

Log in to reply
 

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