Easy Recursive and Iterative solution. Really Easy to understand.


  • 0
    P
    
    public int findMin(int[] nums) { // Recursive solution.
    		return (helper(nums, 0, nums.length - 1));
    	}
    
    	private int helper(int[] nums, int start, int end) { 
    		if (start == end)
    			return nums[start];
    		if (nums[start] < nums[end]) // check if array is already sorted.
    			return nums[start];
    
    		int mid = (start + end) / 2;
    
    		if (nums[mid] >= nums[start]) { // makes sense if you think for a while.
    			return helper(nums, mid + 1, end);
    		}
    
    		else {
    			return helper(nums, start, mid);
    
    		}
    	}
    
    public int findMin(int[] nums) { // Iterative one. Concept is the same.
    		int start = 0;
    		int end = nums.length - 1;
    		
    		while(start != end) {
    			int mid = start + (end - start) / 2;
    			if(nums[start] < nums[end])
    			    return nums[start];
    			if( nums[mid] >= nums[start] )
    				start = mid + 1;
    			else
    				end = mid;
    		}
    		
    		return nums[start];
        }
    

Log in to reply
 

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