In the push group in Sohu, a classmate received the interview phone, memories of one of the several algorithms, there are two groups of friends of the enthusiastic discussion, so write blog to share the idea.
The first question: give you an array, where one element is more than half the total number of the array, to find this element.
The classmate gives the method one: first the array of sorting, and then find the middle of that number, must be the element we are looking for. If you use a quick sort, the average time complexity of the algorithm is O (NLOGN), and the space complexity is O (1).
Group friends to give the method two: with HashMap to solve, key storage elements, value storage number, if the number of more than general, output this element. The complexity of the algorithm is O (n) and the space complexity is O (n).
Here is a time complexity of O (n), the space Complexity of O (1) algorithm, the main ideas are as follows: The first number as the first soldier, guard the position; count = 1; encounter the same element, count++; Encounter different elements, that is, the enemy, Perish, count--; When you encounter the case of Count 0, and the new I value as the soldier guarding the position, continue to the last remaining in the position of the soldiers, is to Shing elements. PS: If the subject requirements to determine whether more than half of the elements exist, as long as the traversal, record the remaining "soldier" is more than half of the number can be. Now post the Java version algorithm:
public class solution{Public
int Majority Element (int[] num) {
int major=num[0], count = 1;
for (int i=1; i<num.length;i++) {
if (count==0) {
count++;
Major=num[i];
} else if (Major==num[i]) {
count++
} else count--;
}
Return major
}
}
This algorithm in the "sword refers to offer" and Leetcode have appeared, so conditional words or can look at the relevant books, write the algorithm.
The second question: give you an array, a target value sum, and look for two numbers inside to make it and target.
This is also a very classic algorithm problem.
The following method excerpt from the "chapter 37 collection By_july".
Method one: Direct poor lift, time complexity O (n^2), obviously is the next unwise.
Method Two: For each a[i], the binary lookup Sum-a[i] is also in the original sequence, each secondary lookup time is spent as O (Logn), the total time complexity O (NLOGN).
Method Three: There is no better way. We can, according to the idea of 2 above, A[i] in a sequence, if a[i]+a[k]=sum, then sum-a[i] is bound to be in the sequence, for example, as follows: Original sequence: 1, 2, 4, 7, 11, 15 minus each number with the input number 15, The corresponding sequence is: 14, 13, 11, 8, 4, 0 the first array with a pointer i from the left end of the array to the right scan, the second array with a pointer J from the right end of the array to scan the left, if the following appears and the same number above, that is a[*i]=a[*j], to find the number of these two. As above, I,j finally found the same number 4 and 11 in the first, and the second sequence, so the two numbers that met the criteria were 4+11=15.
To achieve the complexity of O (N), the first array takes a pointer i from the leftmost end of the array and scans the right. The second array begins with a pointer j from the right end of the array, starting with the first I point to element 1,j Point 0, who refers to the element small, who first moves, as 1 (i) >0 (j) , so I do not move, J move to the left. Then J moves to element 4 to find greater than element 1, so stop moving J, start moving I until I point to 4, when I point to the element that is equal to the element J points to, so that 4 is the first number that satisfies the condition, and then move the i,j and then judge until they reach the boundary.
This method can improve the complexity of the space to reduce the time complexity, if the time complexity requirements, the space is not very strict, but also a good way.
Method Four: Use hash table, given a[i], according to map find Sum-a[i] Whether in the array, space-time are all O (n).
Method Five: If the array is ordered, you can use two pointers i,j, each point to the end of the array, so that i=0,j=n-1, and then i++,j--, successive Judge A[i]+a[j]?=sum, if a moment a[i]+a[j]>sum, you have to find a way to let sum The value is reduced, so at the moment I do not move, j--, if a moment a[i]+a[j]<sum, you have to think of ways to let sum value increase, so the moment i++,j not move. So, when the array is unordered, the time complexity is finally O (n*logn+n) =o (N*LOGN), packages array is ordered, no prior sorting, direct O (n), and space Complexity or O (1), this idea is relative to all of the above ideas of an improvement.
Speaking of this topic, I would like to mention that two-point search is involved in method two, you will write a binary lookup. Know the potential risk of mid= (Low+high)/2? Know that low
From this aspect of Sohu, the main is to study the classical algorithm, so students in peacetime learning important attention to tamp foundation.