Beat 90% Fast Java Solution


  • 0
    M

    As experimented, using ternary operator (a>b?a:b) is much faster than If statement and Math.max(a,b) function.

    public int maxSubArray(int[] nums) {
    
    	int currentMax = nums[0];
    	int finalMax = currentMax;
    	for (int i = 1; i < nums.length; i++) {
    		currentMax = Math.max(nums[i] + currentMax, nums[i]);
    		finalMax = Math.max(currentMax, finalMax);
    	}
    	return finalMax;
    }

Log in to reply
 

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