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. 😛
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. 😛