So this is a general interview question. There is already a topic that I read, but it is dead, and no answer was accepted. In addition, my interests lie in a slightly more limited form of the question, with a few practical applications.
Given a two-dimensional array such that:
- Elements are unique.
- Items are sorted along the x axis and y axis.
- No species prevails, therefore neither sorting is a secondary sorting parameter.
- As a result, the diagonal is also sorted.
- All species can be considered as moving in one direction. That is, they are all ascending, or that they are all descending.
- Technically, I think as long as you have a> / = / <comparator, any complete ordering should work.
- Elements are numerical types with a single cylinder comparator.
- Thus, memory operations are the dominant factor in analyzing large output.
How do you find the item? Only the worst analysis matters.
Solutions I Know About:
Different approaches that are: O (nlog (n)), where you approach each line separately.
O (nlog (n)) with strong best and average performance.
That O (n + m):
Start at the non-extreme corner, which we will take to the bottom right.
Let the target be J. Cur Pos is M.
If M is greater than J, move left.
If M is less than J, move up.
If you cannot do this, you are done, but J is not.
If M is equal to J, everything is ready.
It was originally found elsewhere, most recently stolen from here .
And I believe that I saw one with the worst case O (n + m), but the optimal case is almost O (log (n)).
What interests me:
I am currently happy that the naive break attack always goes to nlog (n). Aggressive attacks as a whole seem to have the optimal worst case O (n + m), and most of them do not end in the early stages of absence. I also wondered, as a result, if the interpolation probe can be no better than a binary probe, and therefore it occurred to me that one might think of this as a given problem of intersection with weak interaction between sets. My mind immediately switched to the intersection of Baeza-Yates , but I did not have time to prepare an adaptation of this approach. However, given my suspicions that the worst case optimality of O (N + M) is provable, I thought that I would just go and ask here to see if anyone could bash the counter argument or hide the recurrence relation to search for interpolation.
source share