AC java recursive solution, anyway to improve it?


  • 0
    F
    public int findMin(int[] nums) {
        if(nums.length <= 1) {return nums[0];}
        if(nums.length == 2) {return Math.min(nums[0],nums[1]);}
        int start = 0, end = nums.length - 1;
        return helper(nums,start,end);
    }
    public int helper(int[] nums, int start, int end){
        if(start >= end) {return nums[start];}
        int mid = start + (end - start)/2;
        if(mid - 1 >= 0 && nums[mid] < nums[mid - 1]) {return nums[mid];}
        if(nums[mid] >= nums[start] && nums[mid] > nums[end] || nums[mid] < nums[start] && nums[mid] > nums[end])
            return helper(nums,mid+1,end);
        else if(nums[mid] > nums[start] && nums[mid] >= nums[end] || nums[mid] > nums[start] && nums[mid] < nums[end])
            return helper(nums,start,mid-1);
        else
            return Math.min(helper(nums,start,mid - 1), helper(nums,mid + 1, end));
    }
    

    }


Log in to reply
 

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