AC java binary search, anyway to improve performance?


  • 0
    F
    public int findMin(int[] nums) {
        if(nums.length <= 1) {return nums[0];}
        int min = Integer.MAX_VALUE;
        int start = 0, end = nums.length - 1;
        while(start < end){
            int mid = (start + end)/2;
        /*handle special case when the mid element is the smallest*/
            if(mid - 1 >= 0 && nums[mid] < nums[mid - 1]) return nums[mid];
        /*standart binary search*/
            if(nums[mid] >= nums[start] && nums[mid] >= nums[end])
                start = mid + 1;
            else
                end = mid - 1;
        }
        return nums[start];
    }
    

    }


Log in to reply
 

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