Any thoughts/opinions on this java O(N) Solution?


  • 0
    Q
       public class Solution {
            public int maxProduct(int[] nums) {
                
                int max=Integer.MIN_VALUE;
                int currentP=1;
                int currentN=1;
                for(int i=0;i<nums.length;i++){
                    currentP*=nums[i];
                    if(currentP>max){
                        max=currentP;
                    }
                    if((currentP<0||currentN!=1)&&i+1<nums.length){
                        currentN*=nums[i+1];
                        if(currentN>max){
                            max= currentN;
                        }
                    }
                    if(currentP==0){
                        currentP=1;
                    }
                    else if (currentN==0){
                        currentN=1;
                    }
                }
                return max;
                
            }
        }

Log in to reply
 

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