Java 1ms binary search solution


  • 0
    L
    public int findMin(int[] nums) {
        return binarySearch(nums,0,nums.length-1);
    }
    private int binarySearch(int[] nums,int l,int h){
        while(l+1 <= h && nums[l] == nums[l+1]) l++;
        while(h-1 >= l && nums[h] == nums[h-1]) h--;
        if(l == h) return nums[l];
        if(l == h-1) return Math.min(nums[l],nums[h]);
        int m = (l+h)/2;
        if(nums[m] > nums[l] && nums[m] > nums[h]) return binarySearch(nums,m,h);
        return binarySearch(nums,l,m);
    }

Log in to reply
 

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