-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.kt
52 lines (48 loc) · 1.35 KB
/
Solution.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
/**
* Created by Inno Fang on 2018/2/28.
*/
/**
* 31 / 31 test cases passed.
* Status: Accepted
* Runtime: 480 ms
*/
class Solution {
fun findKthLargest(nums: IntArray, k: Int): Int {
return partition(nums, k, 0, nums.lastIndex)
}
private fun partition(nums: IntArray, k: Int, start: Int, end: Int): Int {
var lo = start
var hi = end
while (lo < hi) {
while (lo < hi && nums[hi] <= nums[start]) hi--
while (lo < hi && nums[lo] >= nums[start]) lo++
if (lo < hi) (nums[hi] to nums[lo]).run { nums[lo] = first; nums[hi] = second }
}
(nums[start] to nums[hi]).run { nums[start] = second; nums[hi] = first }
return when {
lo == k - 1 -> {
nums[lo]
}
lo > k - 1 -> partition(nums, k, start, lo - 1)
else -> partition(nums, k, lo + 1, end)
}
}
}
/**
* 31 / 31 test cases passed.
* Status: Accepted
* Runtime: 332 ms
*/
class Solution2 {
fun findKthLargest(nums: IntArray, k: Int): Int {
val que = java.util.PriorityQueue<Int>()
nums.forEach {
que.offer(it)
if (que.size > k) que.poll()
}
return que.peek()
}
}
fun main(args: Array<String>) {
Solution2().findKthLargest(intArrayOf(3, 5, 9, 1, 4, 7, 2, 6, 8, 0), 3).let(::println)
}