My solution result is ”Time Limit Exceeded“,but I can get result use My PC


  • 0
    X

    My code is

    public int maxProduct(int[] A) {
    	int result = A[0],temp;
    	
    	for(int i = 0;i<A.length;i++){
    		temp = A[i];
    		for(int j = i + 1 ; j < A.length ; j++){
    			temp = temp * A[j];
    			if(temp > result){
    				result = temp;
    			}
    		}
    		if(A[i] > result){
    			result = A[i];
    		}
    	}
    	
        return result;
    }
    

    Last executed input: [-5,2,4,1,-2,2,-6,3,-1,-1,-1,-2,-3.......,1,1,-1,-1,-1,-1,-1,-1] ,array length 15000

    My PC total run for 108-112ms

    Could anyone tell the correct result?


  • 1
    K

    your answer is correct, but your time complexity is O(N²), the expected solution should be O(N), under 20ms


Log in to reply
 

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