arrays - Find the Smallest Integer Not in a List -
An interesting interview question is that my colleague uses it:
Suppose you have a lot What is not in the ordered list list of long, unsigned 64-bit integers, how would you find the smallest non-negative integer?
Follow: Now that a clear solution to sort is proposed, can you do it faster than O (n log n)?
Follow-up: Your algorithm must be run on computer with 1 GB memory
Clarification: The list is in RAM, although it can consume a large amount of you list Size is given, says N, in advance.
If the datastructure can be mutated in place and supports random access, then you o N) Time and O (1) can do in extra space. Just go through the array in a sequential manner and write a value based on the value in the index for each index, then move any value to that spot and throw the values> n. Then again go through the array where the value does not match the index - this is the smallest value not being in the array. The result is most in comparison to 3 n and uses only some values of the value of the temporary space.
# 1, move each value to the position of its value for the cursor's range (N): Target = Array [cursor] Target while & lt; En and Target! = Array [target]: new_target = array [target] array [target] = target target = new_target # pass 2, find the first position, where the index does not match the value in the category (N): if array [cursor]! = Cursor: Returns the cursor back N
Comments
Post a Comment