Cool Java solution with comments.


  • 0
    P
    public static int findMin(int[] nums) {
    		int low = 0;
    		int high = nums.length - 1;
    
    		while (low != high) {
    			int mid = low + (high - low) / 2;
    			if (nums[low] < nums[high]) // Check if this part is sorted or not.
    				return nums[low];
    			if (nums[mid] == nums[low]) {
    				while (nums[mid] == nums[low] && low != mid + 1) { // increase low till mid + 1.
    					low++;
    				}
    				continue;
    			}
    
    			if (nums[low] > nums[mid]) // Usual BiSearch.
    				high = mid;
    			else
    				low = mid + 1;
    		}
    		return nums[low];
    	}
    

Log in to reply
 

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