< The following Microsoft face questions are all from the network >
< The following answers and analysis are purely personal points of view, shortcomings, but also hope to point out ^_^ >
Q: find the longest arithmetic progression with a length greater than =3 in the array of random numbers
Output arithmetic progression from small to large:
Output [0,0] If no conditions are met
Format:
input [1,3,0,5,-1,6]
Output [ -1,1,3,5]
Requires time complexity and minimal space complexity
analysis : Basic algorithm thinking (using dynamic programming Idea): First, as long as the tolerance of the series and a first term can be determined by a arithmetic progression , so we want to find the longest arithmetic progression tolerance and the first item. Second, in order to facilitate the search for tolerances and the first item, we should sort the original array from small to large , so that the tolerance between the two numbers is incremented, so that we can avoid backtracking to find the first item .
Therefore, in the process of searching for tolerances and the first item, we can divide two or three decision-making stages:
1, If the tolerance is 0, what should be done.
2, If the tolerance is not 0, what should be done to deal with.
3, If the length of the found column is currently the longest to do the corresponding treatment
In view of the above situation, we should choose a suitable data structure--balance sort tree, the set,map,mutiset,multimap in STL is a container with red and black tree structure as the situation , it is undoubtedly very suitable, according to the problem situation, There may be an array with the same elements, so we choose multiset so that no matter how we insert the data, the lookup is more efficient and therefore generally satisfactory.
Finally, there are several time optimizations that are not explained here, the details can be seen in the code. If there are shortcomings, hope can not hesitate to point out. ^_^
My implementation code:
/** Author: Blog:http://blog.csdn.net/zhanxinhang **/#include <iostream> #include <ctime> #include <
Set> using namespace std; void Show_longest_seq (const multiset<int>& myset) {int maxLength = 0, Curr_pos = 0, curr_d = 0, counter=0,i=0; Some auxiliary variables int d_result, a1_result;
Stores the tolerance of the longest arithmetic progression and the first multiset<int>::const_iterator set_it1,set_it2;
/* (subject) Looking for the longest arithmetic progression, worst case time complexity O (n^3) */for (set_it1 = Myset.begin (); Set_it1! = Myset.end ();) {for (set_it2=set_it1,set_it2++; Set_it2! = Myset.end ();)//second-level loop iterates from the next element that the set_it1 refers to {curr_d = *set_it2-*set_it1 ;
Calculate the current tolerance, note that because set is a self-sorting container, from small to large, so curr_d constant is the positive if (Curr_d = = 0)//If the tolerance is 0 {counter = myset.count (*SET_IT1);
Set_it2 = Myset.upper_bound (*set_it1);//(optimization) skips elements equal to set_it1} else {counter = 2;//(optimization) Minimum length requirement is not less than so directly from the beginning to accumulate
while (Myset.find (*set_it1 + counter*curr_d)! = Myset.end ())//Count the number of items ++counter; Set_it2 = Myset.upper_bound (*set_it2);//(Optimization) Skip and *set_It2 equal Elements} if (Counter > MaxLength)//If the new sequence length is greater than maxLength {d_result = curr_d;
A1_result = *set_it1;
MaxLength = counter; }} Curr_pos + = Myset.count (*set_it1);
Calculates the current position of the first-layer loop traversed if (Myset.size ()-curr_pos < MaxLength)//(optimized item) if the remaining elements in the collection are less than the maximum number of sequence lengths, exit the loop break; Set_it1 = Myset.upper_bound (*set_it1); Next set_it1 position and skip the same element}/* Print longest arithmetic progression */if (maxLength <= 2) {cout<< "longest_seq:[0,0]" <<en
dl
} else {cout<< "longest_seq:"; for (i = 0; i<maxlength;
i++) cout<<* (myset.find (A1_result + i*d_result)) << ";
cout<<endl;
}}//blog:http://blog.csdn.net/zhanxinhang////test in main int main () {int a[]={1,3,0,5,-1,6};
Multiset<int> MySet;
Myset.insert (A,A+6);
Show_longest_seq (MySet);
cout<<endl;
int l;
Srand ((unsigned) time (NULL));
for (int j = 0; J < 5; J + +) {myset.clear ();
cout<< "input:[";
L=rand ()%10; for (int i = 0; I < L;
++i) {int element = rand ()%10;
Myset.insert (Element);
cout<<element<< ";
} cout<< ' <<endl;
Show_longest_seq (MySet);
cout<<endl;
} return 0;
}
Attach a test result diagram:
Previous: Daily Microsoft face Test--day 3
======= Welcome to my homepage (Http://blog.csdn.net/zhanxinhang) to has a communication =======