Why I used the same two pointers method but encountered TLE error?


  • 0
    G

    My version could eliminate some unnecessary computations but why my code is much slower?

            int len = height.length;
    	int max = 0; int st = 0; int end = len-1;
    	int minLength = 0; int interval = 0; 
            while(st<end){   
            	interval = end-st;
            	if(height[st]>height[end]){
            		minLength = height[end];            	
            		end--;
            		while(height[end]<=minLength) {
            			end--;
            			if(end<0) break;
            		}
            	}else{
            		minLength = height[st];
            		st++;
            		while(height[st]<=minLength) {
            			st++;
            			if(st>len-1) break;
            		}
            	}
            	max = Math.max(max, minLength*interval);
            }
            return max;

Log in to reply
 

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