“This is the 39th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

[topic address]

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

You are given an array of numbers that may have duplicate elements. It was originally an array in ascending order, rotated once as described above. 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], whose minimum value is 1.

Example 1:

Input: [3,4,5, 2] Output: 1Copy the code

Example 2:

Input: [2,2,2,0,1] output: 0Copy the code

Their thinking

So this is an easy one, and we can use a lazy way of thinking about it

  1. Math.min() + ES6 expansion operator
  2. Don’t pursue efficiency, directly scan through the entire array, after the minimum
  3. Optimize the scan according to the questions in this question. Since this is an ascending array that has been rotated at some point, we first need to determine whether the array has been rotated. The way to determine this is also simple:
    • If the end element is less than or equal to the first element, then we think we’ve rotated the array, and the minimum value must be in the bottom half of the array. We scan the array backwards and forwards until we find a place where the first element is greater than the last, and that’s the minimum value. Of course, it’s possible to go all the way to the beginning of the array, where all the elements in the entire array are the same, so we’re going to add another termination conditioni>=0, and finally return the found element.
    • If the element at the end of the array is greater than the element at the top, then the array is not rotated, and the whole thing is still sorted, then the minimum value must be the first element in the array. Return the first element in the array.

Code implementation

Var minArray = function (numbers) {return math.min (... Var minArray = function (numbers) {let min = 1 for (let I = 1; i < numbers.length; i++) { min = Math.min(min, Var minArray = function (numbers) {const len = numbers. Length // const len = numbers If the first element is greater than or equal to the last element, that means we've done a rotation, If (numbers[0] >= numbers[len-1]) {if (numbers[0] >= numbers[len-1]) {if (numbers[0] >= numbers[len-1]) {if (numbers[0] >= numbers[len-1]) {if (numbers[0] >= numbers[len-1]) { Return numbers[I]} return numbers[0]} return numbers[0]} return numbers[0]}Copy the code

At this point we are done with leetcode- finger Offer 11- the minimum number for the rotation array

If you have any questions or suggestions, please leave a comment! 👏 🏻 👏 🏻 👏 🏻