Did the provided official solution take 0 into consider?


  • 0
    M

    If 0 occurs in the input array, the official solution will not work.


  • 0
    S
    public int maxProduct(int[] nums){
    	if(nums == null || nums.length == 0)
    		return 0;
    	if(nums.length == 1)
    		return nums[0];
    	int localMax = nums[0];
    	int localMin = nums[0];
    	int global = nums[0];
    	for(int i = 1; i < nums.length; i++){
    		int maxCopy = localMax;
    		localMax = Math.max(Math.max(nums[i] * localMax, nums[i]), nums[i] * localMin);
    		localMin = Math.min(Math.min(nums[i] * maxCopy, nums[i]), nums[i] * localMin);
    		global = Math.max(localMax, global);
    	}
    	return global;
    }

  • 0
    X

    Yes. It does.


Log in to reply
 

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