[6] to prove safety offer minimum rotation number of the array [by Python]

Title Description

The beginning of an array of several elements moved to the end of the array, the array we call rotation. A non-descending order of the input array of a rotation of the output rotary smallest element array. For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum. NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

Problem-solving ideas

Ideas 1

Violence Solution: According to the characteristics of a given array, from left to right through the array elements, when first encountered element in the array element of an element than a hour, that element is what we need.

python code

defminNumberInRotateArray(self, rotateArray):
    if not rotateArray:
        return 0
    num = rotateArray[0]
    for i in range(1,len(rotateArray)):
        if rotateArray[i] >= num:
            num = rotateArray[i]
        else:
            return rotateArray[i]
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Ideas 2

1 ideas practice no brain too, the time complexity of O (N). Rotating the array can be considered a sorted array, you can use dichotomy solving the minimum time complexity of control in O (lgN).
Due to the recent use of recursion more, we wanted to solve the problem by recursively instinct. DETAILED idea is positioned to an intermediate position mid variable array, the value at the mid head array compared shrinking array. There are two points to note:
1. Since the array is rotating, reduced to the minimum array size is 2, and the first array element is the maximum value of the array element, the second element is the minimum value in the array elements, and finally return to the second element.
2. When the array contains repeat elements, are specific to the code: when the value is equal to the value at mid array head can not determine the minimum value located mid left or right, can be taken at this time to traverse the way.

Python code

def minNumberInRotateArray(self, rotateArray):
    if not rotateArray:
        return 0
    if len(rotateArray)==2:
        return rotateArray[1]

    mid = int(len(rotateArray)/2)
    if rotateArray[mid] > rotateArray[0]:
        return self.minNumberInRotateArray(rotateArray[mid:])
    elif rotateArray[mid] < rotateArray[0]:
        return self.minNumberInRotateArray(rotateArray[:mid+1])
    else:
        for i in range(1,len(rotateArray)):
            if rotateArray[i] < rotateArray[0]:
                return rotateArray[i]
        return rotateArray[0]
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3 ideas

Ideas 3 2 belonging thoughts of small optimization, can shorten the length of the code, but can not change the time code complexity. Specifically, when the value is equal to the value of the array at the mid of the head, i.e. when the need to traverse the code, instead of a line of code can be used for loop:

def minNumberInRotateArray(self, rotateArray):
    if not rotateArray:
        return 0
    if len(rotateArray)==2:
        return rotateArray[1]

    mid = int(len(rotateArray)/2)
    if rotateArray[mid] > rotateArray[0]:
        return self.minNumberInRotateArray(rotateArray[mid:])
    elif rotateArray[mid] < rotateArray[0]:
        return self.minNumberInRotateArray(rotateArray[:mid+1])
    else:
        return self.minNumberInRotateArray(rotateArray[1:])
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Ideas 4

Because each recursion will need to pass an array, which will undoubtedly take up more storage space, especially when the array is particularly long. Finally, a non-recursive version of the method, only one array constant, varying only the pointer to point to the array.

def minNumberInRotateArray(self, rotateArray):
    left = 0
    right = len(rotateArray)-1
    while left < right:
        mid = int((left+right)/2)
        if rotateArray[mid] > rotateArray[right]:
            left = mid+1
        elif rotateArray[mid] < rotateArray[right]:
            right = mid
        else:
            right -= 1
    return rotateArray[left]
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Although the idea of ​​4 short codes, time complexity and space complexity are relatively low, but if you do not pay attention a little, or will fall into the small pit, for example, if the comparison value and an array of mid left at, what would be different; else if statement points to keep right at the left one, but let pointer left what will happen at the right one, all need a little local attention.

Title Description

Guess you like

Origin blog.csdn.net/qq_33487726/article/details/91872793