为什么使用lo索引解决二分查找问题与使用hi索引不一样?

问题描述 投票:0回答:1

我正在使用二分搜索解决以下问题:-

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

显然最后一行表明我应该使用二分搜索来解决问题。

首先考虑以下示例:-

Example 1:

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Example 2:

Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
Example 3:

Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times. 

这是问题的限制:-

Constraints:

n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
All the integers of nums are unique.
nums is sorted and rotated between 1 and n times.

这是我的第一个解决方案:-

class Solution {
    public int findMin(int[] nums) {
        int lo = 0, hi = nums.length - 1, mid = -1;
        if(nums[lo] <= nums[hi]) return nums[lo]; // Doing this to handle single elements array and the unrotated array without getting into the loop. Even the array will contain distinct element for a single element the first and last element will be the same hence the equality.
        
        while(lo <= hi){
            mid = (lo + hi) >>> 1; // Finding the mid.
            if(mid > 0 && nums[mid] < nums[mid - 1]) return nums[mid];
            if(nums[mid] < nums[hi]) hi = mid - 1; // Using the hi index to ascertain the correct half
            else lo = mid + 1;
        }
        return -1;
    }
}

现在我的问题是,如果您尝试使用 lo 索引,而不是使用 hi 索引,那么如果不添加额外的条件,您就无法做到这一点。

这是额外的条件代码:-

class Solution {
    public int findMin(int[] nums) {
        int lo = 0, hi = nums.length - 1, mid = -1;
        if(nums[lo] <= nums[hi]) return nums[lo];

        while(lo <= hi){
            mid = (lo + hi) >>> 1;
            if(mid - 1 >= 0 && nums[mid] < nums[mid - 1]) return nums[mid];
            else if( mid + 1 <= hi && nums[mid] > nums[mid + 1]) return nums[mid + 1]; // this second condition is extra and was not needed previously.

            else if(nums[mid] < nums[lo]) hi = mid - 1; // Notice that nums[lo] being used
            else lo = mid + 1;
        }
        return -1;
    }
}

我想了解为什么第二个代码在不添加额外条件的情况下不起作用。请帮我解决这个问题? 如果我删除第二个条件,第二个解决方案将失败的测试用例是这样的:-

[4,5,6,7,0,1,2]

它应该给出值 0,但如果删除第二个条件则不是。

java algorithm binary-search
1个回答
0
投票

我真的不能说为什么它不起作用,因为我不明白为什么你认为它应该起作用......

但你只需要一个条件,而不是两个:

class Solution {
    public int findMin(int[] nums) {
        int lo = 0, hi = nums.length - 1, mid = -1;
        final int endVal = nums[hi];
        // find first element <= last element
        while(lo < hi) {
            // round down, because we need mid+1 <= hi
            mid = (lo + hi) >>> 1;
            if (nums[mid] <= endVal) {
                // mid is high enough
                hi = mid;
            } else {
                // mid is not high enough
                lo = mid+1;
            }
        }
        return lo;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.