package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev
# README
215. Kth Largest Element in an Array
Solution idea
Solution - Min Heap
Use min heap
to store k
largest element: every time the top element in min heap is smaller than the current element, pop then push.
Time complexity = $O(n\log k)$
Solution - Binary Search by Value
- 用二分法猜一个数
mid
。每猜一次,拿mid
与nums[]
中每个元素比对,统计nums[]
中大于等于mid
的元素个数。如果符合条件的个数 < k个,说明猜大了,right = mid - 1
;如果符合条件的个数 >= k个,说明mid
有可能是答案,left = mid
。 - CAUTION: 左右边界初始值为
MinInt
和MaxInt
,注意Go中这样的初始值,会导致在计算mid
时溢出。解决办法可以是:left, right := math.MinInt32, math.MaxInt32
或者left, right := math.MinInt / 2, math.MaxInt / 2
。
Time complexity = $O(n\log C)$ where $C = 2^{32}$
Solution - Quick Select
Detailed explanation refers to Resource starting at 17:45
. Use the Rainbow Sort technique (refer to Leetcode 0075)
Partition - 3 Pointers
- Initialize pointers in
nums[left:right]
(inclusive)
i := left
t := left
j := right
- Syntax Meaning:
[left, i) < pivot
[i, t) == pivot
(j, right] > pivot
- General Pattern:
pivot = O
S S S O O X X X L L
left ^ ^ ^ right
i t j
- Loop Execution:
主要移动 t:
if nums[t] < pivot:
nums[t], nums[i] = nums[i], nums[t]
i++
t++
else if nums[t] > pivot:
nums[t], nums[j] = nums[j], nums[t]
j--
else:
t++
- Loop End Condition:
t >= j
- Loop End Pattern:
S S S O O O L L L L
left ^ ^ ^ right
i j t
Find Kth Largest Element
k? k? k?
S S S O O O L L L L
----------- ----------- ----------------
a b c
a := number of elements < (pivot=O)
b := number of elements == (pivot=O)
c := number of elements > (pivot=O)
if c >= k:
Find k-th largest in segment [L]
else if b+c >= k:
k-th largest = O
else:
Find (k-(b+c))-th largest in segment [S]
Time complexity = $O(n)$ on average. Worst case $O(n^2)$