-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2233_maximum_product_after_k_increments.rb
93 lines (70 loc) · 1.58 KB
/
2233_maximum_product_after_k_increments.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
# https://leetcode.com/problems/maximum-product-after-k-increments/
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def maximum_product2233(nums, k)
mod = 10**9 + 7
heap = ::MinHeap.new
nums.each { |num| heap.push(num) }
k.times do
smallest = heap.pop
heap.push(smallest + 1)
end
product = 1
until heap.empty?
num = heap.pop
product = (product * num) % mod
end
product
end
# MinHeap
class MinHeap
# Init
def initialize
@heap = []
end
# @param {Integer} val
# @return {Void}
def push(val)
@heap << val
heapify_up(@heap.size - 1)
end
# @return {Integer}
def pop
return if @heap.empty?
swap(0, @heap.size - 1)
val = @heap.pop
heapify_down(0) unless @heap.empty?
val
end
# @return {Boolean}
def empty? = @heap.empty?
private
# @param {Integer} index
# @return {Void}
def heapify_up(index)
parent = (index - 1) / 2
return unless parent >= 0 && @heap[parent] > @heap[index]
swap(parent, index)
heapify_up(parent)
end
# @param {Integer} index
# @return {Void}
def heapify_down(index)
left = 2 * index + 1
right = 2 * index + 2
smallest = index
smallest = left if left < @heap.size && @heap[left] < @heap[smallest]
smallest = right if right < @heap.size && @heap[right] < @heap[smallest]
return if smallest == index
swap(index, smallest)
heapify_down(smallest)
end
# @param {Integer} i
# @param {Integer} j
# @return {Void}
def swap(i, j)
@heap[i], @heap[j] = @heap[j], @heap[i]
end
end