Medium
Array
Divide and Conquer
Sorting
Heap
215. Kth Largest Element in an Array
Given an integer array nums
and an integer k
, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Can you solve it without sorting?
Example 1:
Example 2:
Constraints:
k
<= nums.length
<= 105nums[i]
<= 104Yen-Chi ChenMon, Aug 14, 2023
partition 函式會回傳一個 pivot,這個 pivot 左邊的元素都比 pivot 大,右邊的元素都比 pivot 小,不斷呼叫 partition 然後每次檢查 pivot 的位置是否為 k - 1,如果是的話就回傳 pivot 的值。
SheepMon, Aug 14, 2023
Jerry Wu14 Aug 2023