diff --git a/README.md b/README.md index 38b374c4..f1b9badc 100644 --- a/README.md +++ b/README.md @@ -748,6 +748,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | | 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) | | 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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index a9f5c7d0..57bceb59 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '9.1.3' + s.version = '9.1.4' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2233_maximum_product_after_k_increments.rb b/lib/medium/2233_maximum_product_after_k_increments.rb new file mode 100644 index 00000000..86dfa313 --- /dev/null +++ b/lib/medium/2233_maximum_product_after_k_increments.rb @@ -0,0 +1,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 diff --git a/test/medium/test_2233_maximum_product_after_k_increments.rb b/test/medium/test_2233_maximum_product_after_k_increments.rb new file mode 100644 index 00000000..e2e1ff25 --- /dev/null +++ b/test/medium/test_2233_maximum_product_after_k_increments.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/2233_maximum_product_after_k_increments' +require 'minitest/autorun' + +class MaximumProductAfterKIncrementsTest < ::Minitest::Test + def test_default_one + assert_equal( + 20, + maximum_product2233( + [0, 4], + 5 + ) + ) + end + + def test_default_two + assert_equal( + 216, + maximum_product2233( + [6, 3, 3, 2], + 2 + ) + ) + end +end