You can use parameterization:
Since we are given in problem k , we can consider it as a constant, therefore, the space O ( k log N ) is admissible.
- Divide the array into
k sections of equal length (O (1) and O ( k log N ) times) to preserve the section boundaries, because each key only needs a log N space) - Find the smallest item in each section (O (
N ) and O ( k log N ) times) to store the smallest items - return the maximum number of k elements that you have (time O (
k ))
Since there is room for more cycles, perhaps the best way to do this. Also note that this will do very poorly in a sorted array ...
jswolf19
source share