1ms clean java recursion solution


  • 0
    S
    public int findMin(int[] nums) {
        return findMin(nums, 0, nums.length-1);
    }
    
    private int findMin(int[] nums, int start, int end){
        if(start == end){
            return nums[start];
        }else{
            int mid = (start+end)/2;
            if(nums[start] < nums[mid] && nums[mid] < nums[end]){
                return nums[start];
            }else{
                int leftMin = findMin(nums, start, mid);
                int rightMin = findMin(nums, mid+1, end);
                return (leftMin<rightMin) ? leftMin : rightMin;
            }
        }
    }

  • 0
    G

    this is not O(log(n)) but O(n) complexity.


  • 0
    S

    its true that in worst case, it take O(n).


Log in to reply
 

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