We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 63f4513 commit e23bfaaCopy full SHA for e23bfaa
solution/0215.Kth Largest Element in an Array/Solution.go
@@ -0,0 +1,35 @@
1
+func adjustHeap(heap []int,i,k int){
2
+ child := i*2+1
3
+ for child < k{
4
+ if child + 1 < k && heap[child] > heap[child+1]{
5
+ child++
6
+ }
7
+ if heap[child] > heap[i]{
8
+ break
9
10
+
11
+ heap[child],heap[i] = heap[i],heap[child]
12
+ i = child
13
+ child = i * 2 + 1
14
15
+}
16
17
18
+func findKthLargest(nums []int, k int) int {
19
20
+ minHeap := make([]int,k)
21
+ copy(minHeap,nums)
22
23
+ for i := k/2-1;i>=0;i--{
24
+ adjustHeap(minHeap,i,k)
25
26
27
+ for i := k;i < len(nums);i++{
28
+ if nums[i] > minHeap[0]{
29
+ minHeap[0] = nums[i]
30
+ adjustHeap(minHeap,0,k)
31
32
33
34
+ return minHeap[0]
35
0 commit comments