Sweet binary search solution in Java:)


  • 0
    E
    public class Solution {
        public int findMin(int[] nums) {
            int r = nums.length;
            while (--r > 0 && nums[r] == nums[r - 1]);
            if (r == 0) return nums[0];
            int lo = 0;
            int hi = r + 1;
            while (hi - lo > 1) {
                int mid = lo + hi >> 1;
                if (nums[mid] >= nums[0])
                    lo = mid;
                else
                    hi = mid;
            }
            return nums[hi % (r + 1)];
        }
    }

Log in to reply
 

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