forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0215-kth-largest-element-in-an-array.kt
52 lines (46 loc) · 1.7 KB
/
0215-kth-largest-element-in-an-array.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
fun findKthLargest(nums: IntArray, k: Int): Int {
val heap = PriorityQueue<Int>()
for (num in nums) {
heap.add(num)
if (heap.size > k)
heap.poll()
}
return heap.peek()
}
// O(n) average time complexity - Quick Select algorithm
fun findKthLargestRecursive(nums: IntArray, k: Int): Int = quickSelect(
array = nums,
startIndex = 0,
endIndex = nums.lastIndex,
k = k
)
private fun quickSelect(
array: IntArray,
startIndex: Int,
endIndex: Int,
k: Int,
): Int {
// find a valid position for pivot such that all values that
// appear before the pivot are lower than the value of pivot,
// and, all values that appear after the pivot are greater
// than the pivot.
@Suppress("UnnecessaryVariable") val pivotIndex = endIndex
var validIndexForPivot = startIndex
for (i in startIndex until endIndex) {
if (array[i] < array[pivotIndex]) {
val temp = array[i]
array[i] = array[validIndexForPivot]
array[validIndexForPivot] = temp
validIndexForPivot++
}
}
// put pivot element in it's sorted position
val temp = array[validIndexForPivot]
array[validIndexForPivot] = array[pivotIndex]
array[pivotIndex] = temp
return if (validIndexForPivot == (array.size - k)) array[validIndexForPivot]
else if (validIndexForPivot > array.size - k) quickSelect(array, startIndex, validIndexForPivot - 1, k)
else quickSelect(array, validIndexForPivot + 1, endIndex, k)
}
}