Why my java code get Time Limit Exceeded ? It passed all tests 1 month ago


  • 0
    C
    public int maxProduct(int[] nums) {
        if(nums.length == 0)
            return 0;
        int curPos = nums[0] > 0 ? nums[0] : 0;
        int curNeg = nums[0] < 0 ? nums[0] : 0;
        int max = nums[0];
        int j = 1;
        while(j < nums.length){
            if(nums[j] >= 0){                
                curPos = Math.max(nums[j], nums[j] * curPos);
                curNeg = Math.min(curNeg * nums[j] , nums[j]);
                max = Math.max(max, curPos);
            }
            else{
                int temp = curPos;
                curPos = Math.max(nums[j] * curNeg, nums[j]);
                curNeg = Math.min(nums[j] * temp, nums[j]);
                max = Math.max(max, curPos);
            }
            j++;
        }
        return max;
    }

  • 0
    H

    I think something goes wrong with leetcode. Lots of problems have TEL errors.


Log in to reply
 

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