Basic Knowledge of web Front-end Knowledge System-Minimum Number of JavaScript Rotating Array

Keywords: less

1. Topic Description

Moving the initial elements of an array to the end of the array is called the rotation of the array.

Input a rotation of an array with a non-recursive emission reduction order, and output the smallest element of the rotation array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1.

2. Thoughts on Problem Solving

The simplest one must be traversed from beginning to end, and the complexity is O(N). This method does not take advantage of the "rotating array" feature.

With the idea of binary search, the time complexity can be reduced to O (log (N).

The location of the minimum element can be determined by the following method, and then the pointer can be moved to narrow the range:

  • If the element corresponding to the intermediate pointer is greater than or equal to the left element, then the intermediate element is in the original incremental array and the minimum value is on the right side.

  • If the element corresponding to the intermediate pointer is less than the element on the right, the intermediate element is in the moving incremental array and the minimum value is on the left.

  • In special cases, if the three elements are equal, the position of the minimum element can not be judged, and it will degenerate into ordinary traversal.

3. code

First of all, the last section of the dichotomy search and implementation ideas:

/**
 * Two points search
 * @param {Array} arr
 * @param {*} elem
 */
function binarySearch(arr, elem) {
  let left = 0,
    right = arr.length - 1,
    mid = -1;

  while (left <= right) {
    // Note that <: Consider the case where there is only one element left
    mid = Math.floor((left + right) / 2);

    if (arr[mid] === elem) {
      return true;
    }

    if (elem < arr[mid]) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }

  return false;
}

/**
 * Test code
 */
console.log(binarySearch([1, 2], 2));
console.log(binarySearch([1, 2], -1));
console.log(binarySearch([1, 2, 10], 2));

With the help of the idea of dichotomy search, write the code of this topic:

/**
 * Find the minimum value in order in arr[left, right]
 * @param {Array} arr
 * @param {Number} left
 * @param {Number} right
 */
function orderSearchMin(arr, left, right) {
  let min = arr[left];

  for (let i = left + 1; i <= right; ++i) {
    arr[i] < min && (min = arr[i]);
  }

  return min;
}

/**
 * Finding the Minimum Value by Dichotomy in Rotating Array
 * @param {Array} arr
 */

function minNumberInRotateArray(arr) {
  if (!Array.isArray(arr) || !arr.length) {
    return 0;
  }

  let left = 0,
    right = arr.length - 1,
    mid = null;

  while (left < right) {
    if (right === 1 + left) {
      return arr[right];
    }

    mid = Math.floor((left + right) / 2);

    if (arr[mid] === arr[left] && arr[mid] === arr[right]) {
      // Unable to determine the minimum position
      return orderSearchMin(arr, left, right);
    }

    if (arr[mid] >= arr[left]) {
      // The minimum is on the right.
      left = mid;
    } else if (arr[mid] <= arr[right]) {
      // The minimum is on the left.
      right = mid;
    }
  }

  return arr[right];
}

/**
 * Test code
 */

console.log(minNumberInRotateArray([3, 4, 5, 1, 2]));
console.log(minNumberInRotateArray([2, 3, 4, 5, 1]));
console.log(minNumberInRotateArray([2, 2, 2, 1, 1, 2]));
console.log(minNumberInRotateArray([1]));
web Front end development learning Q-q-u-n:  767273102 ,Share learning methods and small details that need attention, and keep updating the latest teaching and learning methods (detailed front-end project teaching video)

Posted by mystery_man on Sun, 06 Oct 2019 03:32:40 -0700