Share my short Java O(N) solution


  • 0
    J
    public int maxSubArray(int[] nums) {
    
    	int maxSum = Integer.MIN_VALUE, sum = 0;
    	
    	for (int i=0; i<nums.length; i++){
    		sum += nums[i];
    		maxSum = Math.max(sum, maxSum);
    		if (sum < 0)	sum=0;
    	}
    	
    	return maxSum;
    }

Log in to reply
 

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