divide and conquer recursive , need help to more elegant code


  • 0
    R

    public class KthLargestNum {

     public static int findKthLargest(int[] nums, int k) {
            
    	return  findHelper(nums,0,1,k,0);
     }
     
     public static int findHelper(int[] nums,int holder,int runner,int k,int counter){
    	int result = 0;
    	
    	if(counter > k ){
    		return -1;
    	}
    	if(nums.length -1  < runner|| nums.length -1 < holder){
    		if(counter +1 == k){
    			   return nums[holder];
    		}
    		return -1;
    	}
    	if(nums[holder] < nums[runner]){
    		result = findHelper(nums,holder,runner+1,k,counter+1);	
    	}else{
    		result = findHelper(nums,holder,runner+1,k,counter);	
    	}
    	if(result!=-1){
    		return result;
    	}
    	return findHelper(nums,holder+1,0,k,0);
     }
    
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	int[] intArray = new int[] {3,2,1,5,6,4};
    	System.out.println(findKthLargest(intArray,1));
    	
    }
    

    }


Log in to reply
 

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