-
Notifications
You must be signed in to change notification settings - Fork 30
/
find_kth_largest_in_num.rb
51 lines (47 loc) · 1.17 KB
/
find_kth_largest_in_num.rb
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
# Q-215: quick select for kth largest
# NOTE: partition method changed on comparison
def find_kth_largest(nums, k)
left, right = 0, nums.size - 1
while (q = partition(nums, left, right)) != k - 1
if q > k - 1
right = q - 1
else
left = q + 1
end
end
nums[q]
end
def partition(nums, left, right)
x = rand(right - left) + left
nums[x], nums[right] = nums[right], nums[x]
i = left - 1
(left...right).each do |j|
next if nums[j] < nums[right]
i += 1
nums[i], nums[j] = nums[j], nums[i]
end
nums[i + 1], nums[right] = nums[right], nums[i + 1]
i + 1
end
p find_kth_largest([3, 2, 1, 5, 6, 4], 2) == 5
def max_heapify(arr, i, size)
l = i * 2 + 1
r = i * 2 + 2
larger = i
larger = l if l < size && arr[l] > arr[larger]
larger = r if r < size && arr[r] > arr[larger]
return if larger == i
arr[i], arr[larger] = arr[larger], arr[i]
max_heapify(arr, larger, size)
end
def find_kth_largest_heap_sort(nums, k)
(nums.size / 2).downto(0) do |i|
max_heapify(nums, i, nums.size)
end
k.times do |i|
n = nums.size - 1 - i
nums[0], nums[n] = nums[n], nums[0]
max_heapify(nums, 0, n)
end
nums[nums.size - k]
end