Find Top k Elements
2022年5月1日小于 1 分钟
Find Top k Elements
Question
Given an unsorted array of integers which might have duplicates, return the top k integers in non-ascending order.
topK([1,10,8,9,10,2,3,4,8,8,6], 4)
// [10, 10, 9, 8]
What is the time & space cost of your code ? Could you do better ?
Code
/*
* @param {number[]} arr
* @param {number} k
* @returns {number[]}
*/
function topK(arr, k) {
// your code here
}