Very simple Java Solution Time:O(n) Space: O(1)


  • 0
    J
    public int maxSubArray(int[] nums) {
        int sum = 0;  int max = Integer.MIN_VALUE;
        for(int num : nums){
            if(sum + num < num) sum = 0;
            sum = sum + num;
            if( sum > max) max = sum;
            
        }
        return max;
        
    }

Log in to reply
 

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