Beat 98% JAVA


  • 0
    H
    public class Solution {
        public int maxProduct(int[] nums) {
            int accuNeg[] = new int[nums.length];
            int accuPos[] = new int[nums.length];
            for(int i = 0; i < nums.length; i++){
                accuNeg[i] = 0;
                accuPos[i] = 0;
            }
            int max = nums[0];
            if(nums[0] < 0){
                accuNeg[0] = nums[0];
            }else{
                accuPos[0] = nums[0];
            }
            for(int i = 1; i < nums.length; i++){
                if(nums[i] < 0){
                   accuPos[i] = accuNeg[i-1] * nums[i];
                   accuNeg[i] = (accuPos[i-1] == 0)? nums[i] : accuPos[i-1] * nums[i];
                }else{
                   accuPos[i] = (accuPos[i-1] == 0)? nums[i] : accuPos[i-1] * nums[i];
                   accuNeg[i] = accuNeg[i-1] * nums[i];
                }
                max = (max > accuPos[i])? max:accuPos[i];
            }
            return max;
        }
    }
    

Log in to reply
 

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