Java O(n) and in place solution


  • 1
    Z

    public int maxSubArray(int[] nums) {

        int length = nums.length;
        
        int max = Integer.MIN_VALUE;
        int minsum = 0;
        int sum = 0;
        
        for(int i=0;i<length;i++){
            sum+=nums[i];
            max = Math.max(max,sum-minsum);
            minsum = Math.min(sum,minsum);
        }
        
        return max;
    }

Log in to reply
 

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