7

Telephone interview algorithm question and answer record

Q: An array, which is incremented first and then decremented, returns the largest number, how to achieve it?
Answer: Traverse the ith number in turn. If the ith number is greater than the ith + 1 number, then return the ith number, which is the maximum value. (It's so simple, a freshman can do it)
Q: Can it still be optimized?
Answer: Ah. . . . . .
image.png
(The algorithm with a faster time complexity than o(n) is o(logn), which should be the method of dichotomy. The key is how to judge whether a number is the largest observation array, 1 2 3 4 5 4 3, if if The ith number is less than the ith + 1th number, then it means that the ith number is in the increasing part, and the maximum number is in the second half. We reduce the range by half and then go to the second half to find the maximum number. If the ith number is greater than the th i + 1 number, then he may be the largest number, or it may be in the decreasing part. If it is in the decreasing part, it means that the largest number is in the first half. We narrow the range by half and then go to the first half to find the largest number. But how to judge whether it is What about the largest number? Then add another judgment condition. Let's look at the law of the ith number, the ith - 1th number, and the ith + 1th number. If the three numbers increase in turn, then the ith number Just in the increasing part, if the three numbers are decreasing in turn, then the i-th number is in the decreasing part. If the three numbers are increasing first and then decreasing in sequence, then the i-th number is the largest number.)
Use binary search, set the middle number table as i, if the i-th number is greater than the i-1th number and the i+1th number is greater than the i-th number, then the i-th number is in the increasing sequence part, after use The half array continues the search; if the ith number is less than the ith - 1th number and the ith + 1th number is greater than the ith number, then the ith number is in the descending sequence part, and the first half of the array is used to continue the search; If the ith number is greater than the i - 1th number and the ith number is greater than the i + 1th number, then this number is the maximum number.
Q: What is the time complexity?
Answer: logn.
Q: How did you do the sum of three numbers in your written test (given an unordered array and a target value, judge whether three numbers can be found in the array, and make the sum of the three numbers the target value), and what is the time complexity? ?
Answer: The HashMap is used to store the array's key = value, value = value in the following table method, and then use a double loop, one layer i loop and one j loop, to determine whether the HashMap contains (target value- i-th number - j-th number). The time complexity is O(n^2).
Q: Is there any other way?
Answer: Ah. . . . . .
image.png
(For an unordered array, first quickly arrange it into an ordered array with a time complexity of o(nlogn), and then define three pointers i, j, and k to be 0 initially, if the ith number + the jth number + The kth number is less than the target number, then k++, if the ith number + the jth number + the kth number is greater than the target number, then k++ is meaningless, then go to move j, no, The time complexity of doing this is still n^3, which is nothing more than pruning. Think again.
image.png
For a fixed i, it can be faster if j and k are initialized to a close value. There, fix i as 0, set j as 1, and k as the maximum subscript value n - 1, if the i-th number + j-th number + k-th number is greater than the target value, then set j++, if the i-th number is greater than the target value The number + the jth number + the kth number is less than the target value, then k--, when k == j, jump out of the loop
image.png
Judge i++ again).

Set three pointers i = 0; j = i + 1; k = n - 1; fix i, use the double pointer method for finding the second number and the third number, if the sum of the corresponding numbers of the three pointers is less than the target number , move the j pointer back one place, and if the sum of the corresponding numbers of the three pointers is greater than the target number, move the k pointer forward one place. if j == k. Then jump out of the loop, i++, and then make a judgment. This time complexity is O(n^2).
Q: Is there any other way?
A: (Interviewer, don't go too far!) Unexpected.

Summarize

In fact, the general method of algorithm problems is relatively fixed. For example, for the array part, you can use HashMap, dichotomy, double pointer and other methods to optimize the time complexity. But it's not simply whether you know these methods, but whether you can make slight modifications based on these methods according to the requirements of the question. For example, in the first algorithm question, he used the dichotomy method, but the judgment conditions and To narrow the second half and narrow the first half, you need to think for yourself according to the requirements of the topic.
For another example, if you give an ordered array and a target value, and return the smallest number in the array that is greater than the target value, use the dichotomy method, which is also a change to the dichotomy method.
The second algorithm problem uses double pointers, but uses double pointer search in a loop.


小强Zzz
1.2k 声望32 粉丝