Skip to content

Commit 35a7a7f

Browse files
authored
2025-03-28 v. 9.1.4: added "2233. Maximum Product After K Increments"
2 parents 70dbab2 + 10e2b0c commit 35a7a7f

4 files changed

+122
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
748748
| 2196. Create Binary Tree From Descriptions | [Link](https://leetcode.com/problems/create-binary-tree-from-descriptions/) | [Link](./lib/medium/2196_create_binary_tree_from_descriptions.rb) | [Link](./test/medium/test_2196_create_binary_tree_from_descriptions.rb) |
749749
| 2221. Find Triangular Sum of an Array | [Link](https://leetcode.com/problems/find-triangular-sum-of-an-array/) | [Link](./lib/medium/2221_find_triangular_sum_of_an_array.rb) | [Link](./test/medium/test_2221_find_triangular_sum_of_an_array.rb) |
750750
| 2225. Find Players With Zero or One Losses | [Link](https://leetcode.com/problems/find-players-with-zero-or-one-losses/) | [Link](./lib/medium/2225_find_players_with_zero_or_one_losses.rb) | [Link](./test/medium/test_2225_find_players_with_zero_or_one_losses.rb) |
751+
| 2233. Maximum Product After K Increments | [Link](https://leetcode.com/problems/maximum-product-after-k-increments/) | [Link](./lib/medium/2233_maximum_product_after_k_increments.rb) | [Link](./test/medium/test_2233_maximum_product_after_k_increments.rb) |
751752
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
752753
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
753754
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '9.1.3'
8+
s.version = '9.1.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/maximum-product-after-k-increments/
4+
# @param {Integer[]} nums
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def maximum_product2233(nums, k)
8+
mod = 10**9 + 7
9+
10+
heap = ::MinHeap.new
11+
nums.each { |num| heap.push(num) }
12+
13+
k.times do
14+
smallest = heap.pop
15+
heap.push(smallest + 1)
16+
end
17+
18+
product = 1
19+
20+
until heap.empty?
21+
num = heap.pop
22+
product = (product * num) % mod
23+
end
24+
25+
product
26+
end
27+
28+
# MinHeap
29+
class MinHeap
30+
# Init
31+
def initialize
32+
@heap = []
33+
end
34+
35+
# @param {Integer} val
36+
# @return {Void}
37+
def push(val)
38+
@heap << val
39+
40+
heapify_up(@heap.size - 1)
41+
end
42+
43+
# @return {Integer}
44+
def pop
45+
return if @heap.empty?
46+
47+
swap(0, @heap.size - 1)
48+
val = @heap.pop
49+
50+
heapify_down(0) unless @heap.empty?
51+
52+
val
53+
end
54+
55+
# @return {Boolean}
56+
def empty? = @heap.empty?
57+
58+
private
59+
60+
# @param {Integer} index
61+
# @return {Void}
62+
def heapify_up(index)
63+
parent = (index - 1) / 2
64+
65+
return unless parent >= 0 && @heap[parent] > @heap[index]
66+
67+
swap(parent, index)
68+
heapify_up(parent)
69+
end
70+
71+
# @param {Integer} index
72+
# @return {Void}
73+
def heapify_down(index)
74+
left = 2 * index + 1
75+
right = 2 * index + 2
76+
smallest = index
77+
78+
smallest = left if left < @heap.size && @heap[left] < @heap[smallest]
79+
smallest = right if right < @heap.size && @heap[right] < @heap[smallest]
80+
81+
return if smallest == index
82+
83+
swap(index, smallest)
84+
heapify_down(smallest)
85+
end
86+
87+
# @param {Integer} i
88+
# @param {Integer} j
89+
# @return {Void}
90+
def swap(i, j)
91+
@heap[i], @heap[j] = @heap[j], @heap[i]
92+
end
93+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2233_maximum_product_after_k_increments'
5+
require 'minitest/autorun'
6+
7+
class MaximumProductAfterKIncrementsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
20,
11+
maximum_product2233(
12+
[0, 4],
13+
5
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
216,
21+
maximum_product2233(
22+
[6, 3, 3, 2],
23+
2
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)