log(n) solution


  • 0
    F
    public int findMin(int[] nums) {
            if (nums.length == 0) return 0;
            return findMin(nums, 0, nums.length-1);
        }
        
        private int findMin(int[] nums, int left, int right) {
            int mid = 0;
            while (left < right) {
                mid = (left+right)/2;
                if (nums[mid] > nums[right]) {
                    left = mid+1;
                } else if (nums[mid] < nums[right]){
                    right = mid;
                } else {
                    if (nums[mid] != nums[left]) right = mid;
                    else {
                        int leftMin = findMin(nums, left, mid);
                        int rightMin = findMin(nums, mid+1, right);
                        return Math.min(leftMin, rightMin);
                    }
                }
            }
            return nums[left];
        }

Log in to reply
 

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