Interview 8: Minimum number of rotated arrays
title: Move the first element of an array to the end of the array, We call this the rotation of the array. Enter a from small to large , Outputs the smallest element of the rotated array. For example, array {3, 4, 5, 1, 2} {1, 2, 3, 4, 5} 1 cannot iterate directly over an array to solve.)
Submission URL: http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159
or http://ac.jobdu.com/problem.php?pid=1386
time limit:1 sec Memory limit: +M Special question: no submit:7633 solution: 1686
-
-
Title Description:
-
Moves the first element of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting 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}, and the minimum value of the array is 1.
-
Input:
-
The input may contain multiple test samples, for each test case,
The first behavior of the input is an integer n (1<= n<=1000000): The number of elements that represent the rotated array.
The second line of input includes n integers, where the range of each integer A is (1<=a<=10000000).
-
Output:
-
corresponding to each test case,
Outputs the smallest element in the rotated array.
-
OJ Sample input: ( for C + + submission:vector<int> Rotatearray
)
-
3 4 5) 1 2
-
Nine-degree OJ Sample input:
-
5
-
3 4 5) 1 2
-
Sample output:
- 1
Analysis:
The most intuitive solution to this problem is not difficult. By iterating through the array one time, you can find the smallest element, and the time complexity is obviously O (n). But this idea does not take advantage of the characteristics of rotating arrays, we should be able to find a better solution.
Notice that the rotated array can actually be divided into two sorted sub-arrays, and the elements of the preceding sub-arrays are greater than or equal to the elements of the post-face array. We can also notice that the smallest element is just the dividing line of these two sub-arrays. We try to find the smallest element with the idea of a binary search method, with the time complexity O (log n).
niu ke net OJ AC Code:
#include <cstdio> #include <vector>using namespace Std;class solution {Public:int Minnumberinrotatearray ( Vector<int> Rotatearray) {if (rotatearray.size () = = 0) return 0; The input is an empty array of int low = 0, and high = Rotatearray.size ()-1; int mid;if (Rotatearray[low] < Rotatearray[high]) return Rotatearray[low]; The input is a fully rotated array while (Rotatearray[low] >= Rotatearray[high])//input is a partially rotated array {mid = (low + high)/2; if (high-low = = 1) return Rotatearray[high]; When low and high two pointers are adjacent, the minimum value is found, i.e. the first value of the right sequence if (rotatearray[low] = = Rotatearray[mid] && Rotatearray[mid] = = Rotatearray[high]) {//when Rotatearray[low], Rotatearray[high], Rotatearray[mid] Three are equal,//cannot determine whether the intermediate element belongs to the front or the rear Incremented subarray of polygons, only order lookup int min = Rotatearray[low]; for (int i = low + 1, I <= high; i++) min = rotatearray[i]<min? Rotatearray[i]: min; return min;} if (Rotatearray[low] <= rotatearray[mid]) low = mid; The intermediate element is in the preceding incrementing subarray, and at this point the smallest element is at the back of the middle element, else High = mid; The intermediate element is at the back of the incrementing subarray, and the smallest element is at the front of the Middle element} return Rotatearray[low]; }};//the following for the test int main () {solution sol;vector<int> vec1={3,4,5,1,1,2};vector<int> vec2={1,1,2,3,4,6};int Res1 = Sol.minnumberinrotatearray (VEC1); int res2 = Sol.minnumberinrotatearray (VEC2);p rintf ("%d\n", Res1);p rintf ("%d\ n ", Res2); return 0;}
According to the idea of "the Sword of Offer" in version 2014, write the Code:
#include <cstdio> #include <vector>using namespace Std;class solution{public:int Minnumberinrotatearray ( Vector<int> rotatearray) {int size = Rotatearray.size ();//input is an empty array if (size = = 0) return 0; int left = 0, mid = 0, right = size-1;if (Rotatearray[left] < rotatearray[right]) return rotatearray[left]; The input is a fully rotated array while (Rotatearray[left] >= rotatearray[right])//input is a partially rotated array {if (right-left = = 1)//demarcation point {mid = right; Break } mid = (right + left)/2; if (rotatearray[left] = = Rotatearray[right] && rotatearray[left] = Rotatearray[mid]) {//order to find the minimum value int min = Rotatearray[left]; for (int i = left + 1, I <= right; i++) min = rotatearray[i]<min? Rotatearray[i]: min; return min; }//Intermediate element in the preceding increment subarray, at which point the smallest element is at the back of the middle element if (Rotatearray[mid] >= rotatearray[left]) left = mid; The intermediate element is positioned at the back of the incrementing sub-array, and the smallest element is at the front of the intermediate element, or right = mid; } return Rotatearray[mid]; }}; The following is the test int main () {solution sol;vector<int> Vec1={3,4,5,1,1,2};int res1 = Sol.minnumberinrotatearray (VEC1); printf ("%d\n", res1); return 0;}
Sword point 8: The smallest number of rotating arrays