Minimum number of JS rotated array

Rotation array definition: move the first few elements of an array to the end of the array, we call it the rotation of the array.

Given an array numbers with possible duplicate element values, it was originally an array in ascending order, and a rotation was performed as above. Please return the smallest element of the rotated array. For example, the array [3,4,5,1,2] is a rotation of [1,2,3,4,5], the minimum value of the array is 1.

  • Example 1:
    Input: [3,4,5,1,2]
    Output: 1
  • Example 2:
    Input: [2,2,2,0,1]
    Output: 0
  • problem solving ideas

According to the meaning of the question, we can solve it through brute force at the first time, use a variable to record the minimum value encountered during the current traversal process, and then return the minimum value after the traversal is completed.
However, such violence is not elegant!
Why do you say that, because we know that the array is rotated, so during the traversal process, as long as there is a number that is less than numbers[0], then the number must be the minimum value, you can taste it carefully.
Hey, so, even if it is a violent explosion, there must be a bright spot worthy of praise.
Well, enough gossip, brute force is not our goal, our goal is to reduce the complexity of the algorithm as much as possible, so we use the dichotomy method here.

First, create two pointers left and right to point to the first and last numbers of numbers respectively, and then calculate the middle index value middle between the two pointers, and then we will encounter the following three situations:

  1. middle > right: It means that the minimum value must be on the right side of middle, so left is moved to the position of middle + 1.
  2. middle < right : It means that the minimum value must be on the left side of middle or middle, so right moves to the position of middle.
  3. middle is neither greater than the value of the left pointer nor less than the value of the right pointer, which means that middle may be equal to the value of the left pointer or the value of the right pointer. At this time, we can only decrement the right pointer to find the minimum value one by one. .

Method 1: Dichotomy

/**
 * @param {number[]} numbers
 * @return {number}
 */
var minArray = function(numbers) {
    
    
    let left=0;
    let right=numbers.length-1;
    while(left<right){
    
    
        var mid=left + Math.floor((right-left)/2);
        if(numbers[mid]>numbers[right]){
    
    
            left=mid+1;
        }else if(numbers[mid]<numbers[right]){
    
    
            right=mid;
        }else{
    
    
            right--;
        }
    }
    return numbers[left];
};

Method 2: blasting method

/**
 * @param {number[]} numbers
 * @return {number}
 */
// 暴破【不推荐】
var minArray = function(numbers) {
    
    
    for(let i = 0; i < numbers.length; i++){
    
    
        if(numbers[i] < numbers[0]){
    
    
            return numbers[i];
        }
    }
    return numbers[0];
}

Welcome to pay attention to the minimum number of JS rotation array in Jianshu Tongwen

Guess you like

Origin blog.csdn.net/qq_38970408/article/details/122251405