find the K-th largest element in an unsorted array
2022年5月1日小于 1 分钟
find the K-th largest element in an unsorted array
Question
You are given an unsorted array of numbers, which might have duplicates, find the K-th largest element.
The naive approach would be sort it first, but it costs O(nlogn)
, could you find a better approach?
Maybe you can recall what is happening in Quick Sort or Priority Queue
Code
/**
* @param {number[]} arr
* @param {number} k
*/
function findKThLargest(arr, k) {
// your code here
}