My Java Solution using Binary Search


  • 1
    R
    public class Solution {
        public int findMin(int[] nums) {
            int ans = Integer.MAX_VALUE;
            int start = 0,
                end = nums.length - 1;
    
            while(start <= end){
                int mid = (start + end) / 2;
                ans = Math.min(ans , nums[mid]);
                if(nums[mid] > nums[end]){
                    ans = Math.min(ans , nums[start]);
                    start = mid +1;
                }
                else if(nums[start] < nums[mid]){
                    ans = Math.min(ans , nums[start]);
                    end = mid - 1;
                }
                else start++;
                
            }
            return ans;
        }
    }

Log in to reply
 

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