Sharing my Java DP Solution


  • 0
    M
    public int maxSubArray(int[] nums) {
            if (nums == null || nums.length == 0) {
    			return 0;
    		}
    		
    		int max = nums[0];
    		int maxSum = nums[0];
    		
    		for (int i = 1; i < nums.length; i++) {
    			maxSum = Math.max(nums[i], nums[i] + maxSum);
    			max = Math.max(max, maxSum);
    		}
    		
    		return max;
        }
    

Log in to reply
 

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