Given a list of *n* numbers, get the top *k* ones in O(*n*) time.

This problem is too easy to solve in O(*n* log *n*) time

def top_k_n_log_n(l, k):
sl = sorted(l)
return sl[-k:]

but o(*n* log *n*) solutions can be presented for partial credit. ðŸ˜›

### Like this:

Like Loading...

*Related*

For example?

Edited to format the code in a nicer way.Nice

~~abuse of the rules~~solution! ðŸ˜›Before reading your comment, I wasn’t aware that

`std::nth_element`

partitioned the data.[…] [Answer to the problem in Top k.] […]