From ee4420797b35db7963dd58bea83e13e55b8b4ec4 Mon Sep 17 00:00:00 2001 From: fartem Date: Tue, 14 Jan 2025 20:06:08 +0300 Subject: [PATCH] 2025-01-14 v. 8.0.5: added "1770. Maximum Score from Performing Multiplication Operations" --- README.md | 47 ++++++++++--------- leetcode-ruby.gemspec | 2 +- ...om_performing_multiplication_operations.rb | 22 +++++++++ ...om_performing_multiplication_operations.rb | 27 +++++++++++ 4 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb create mode 100644 test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb diff --git a/README.md b/README.md index 31a3a7cc..552c57f8 100644 --- a/README.md +++ b/README.md @@ -651,26 +651,27 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). ### Hard -| Name | Link to LeetCode | Link to solution | Link to tests | -| --------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------ | -| 4. Median of Two Sorted Arrays | [Link](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Link](./lib/hard/4_median_of_two_sorted_arrays.rb) | [Link](./test/hard/test_4_median_of_two_sorted_arrays.rb) | -| 10. Regular Expression Matching | [Link](https://leetcode.com/problems/regular-expression-matching/) | [Link](./lib/hard/10_regular_expression_matching.rb) | [Link](./test/hard/test_10_regular_expression_matching.rb) | -| 23. Merge k Sorted Lists | [Link](https://leetcode.com/problems/merge-k-sorted-lists/) | [Link](./lib/hard/23_merge_k_sorted_lists.rb) | [Link](./test/hard/test_23_merge_k_sorted_lists.rb) | -| 25. Reverse Nodes in k-Group | [Link](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Link](./lib/hard/25_reverse_nodes_in_k_group.rb) | [Link](./test/hard/test_25_reverse_nodes_in_k_group.rb) | -| 30. Substring with Concatenation of All Words | [Link](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [Link](./lib/hard/30_substring_with_concatenation_of_all_words.rb) | [Link](./test/hard/test_30_substring_with_concatenation_of_all_words.rb) | -| 32. Longest Valid Parentheses | [Link](https://leetcode.com/problems/longest-valid-parentheses/) | [Link](./lib/hard/32_longest_valid_parentheses.rb) | [Link](./test/hard/test_32_longest_valid_parentheses.rb) | -| 41. First Missing Positive | [Link](https://leetcode.com/problems/first-missing-positive/) | [Link](./lib/hard/41_first_missing_positive.rb) | [Link](./test/hard/test_41_first_missing_positive.rb) | -| 115. Distinct Subsequences | [Link](https://leetcode.com/problems/distinct-subsequences/) | [Link](./lib/hard/115_distinct_subsequences.rb) | [Link](./test/hard/test_115_distinct_subsequences.rb) | -| 126. Word Ladder II | [Link](https://leetcode.com/problems/word-ladder-ii/) | [Link](./lib/hard/126_word_ladder_ii.rb) | [Link](./test/hard/test_126_word_ladder_ii.rb) | -| 233. Number of Digit One | [Link](https://leetcode.com/problems/number-of-digit-one/) | [Link](./lib/hard/233_number_of_digit_one.rb) | [Link](./test/hard/test_233_number_of_digit_one.rb) | -| 239. Sliding Window Maximum | [Link](https://leetcode.com/problems/sliding-window-maximum/) | [Link](./lib/hard/239_sliding_window_maximum.rb) | [Link](./test/hard/test_239_sliding_window_maximum.rb) | -| 297. Serialize and Deserialize Binary Tree | [Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Link](./lib/hard/297_serialize_and_deserialize_binary_tree.rb) | [Link](./test/hard/test_297_serialize_and_deserialize_binary_tree.rb) | -| 336. Palindrome Pairs | [Link](https://leetcode.com/problems/palindrome-pairs/) | [Link](./lib/hard/336_palindrome_pairs.rb) | [Link](./test/hard/test_336_palindrome_pairs.rb) | -| 363. Max Sum of Rectangle No Larger Than K | [Link](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [Link](./lib/hard/363_max_sum_of_rectangle_no_larger_than_k.rb) | [Link](./test/hard/test_363_max_sum_of_rectangle_no_larger_than_k.rb) | -| 458. Poor Pigs | [Link](https://leetcode.com/problems/poor-pigs/) | [Link](./lib/hard/458_poor_pigs.rb) | [Link](./test/hard/test_458_poor_pigs.rb) | -| 871. Minimum Number of Refueling Stops | [Link](https://leetcode.com/problems/minimum-number-of-refueling-stops/) | [Link](./lib/hard/871_minimum_number_of_refueling_stops.rb) | [Link](./test/hard/test_871_minimum_number_of_refueling_stops.rb) | -| 895. Maximum Frequency Stack | [Link](https://leetcode.com/problems/maximum-frequency-stack/) | [Link](./lib/hard/895_maximum_frequency_stack.rb) | [Link](./test/hard/test_895_maximum_frequency_stack.rb) | -| 936. Stamping The Sequence | [Link](https://leetcode.com/problems/stamping-the-sequence/) | [Link](./lib/hard/936_stamping_the_sequence.rb) | [Link](./test/hard/test_936_stamping_the_sequence.rb) | -| 968. Binary Tree Cameras | [Link](https://leetcode.com/problems/binary-tree-cameras/) | [Link](./lib/hard/968_binary_tree_cameras.rb) | [Link](./test/hard/test_968_binary_tree_cameras.rb) | -| 1106. Parsing A Boolean Expression | [Link](https://leetcode.com/problems/parsing-a-boolean-expression/) | [Link](./lib/hard/1106_parsing_a_boolean_expression.rb) | [Link](./test/hard/test_1106_parsing_a_boolean_expression.rb) | -| 1220. Count Vowels Permutation | [Link](https://leetcode.com/problems/count-vowels-permutation/) | [Link](./lib/hard/1220_count_vowels_permutation.rb) | [Link](./test/hard/test_1220_count_vowels_permutation.rb) | +| Name | Link to LeetCode | Link to solution | Link to tests | +| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | +| 4. Median of Two Sorted Arrays | [Link](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Link](./lib/hard/4_median_of_two_sorted_arrays.rb) | [Link](./test/hard/test_4_median_of_two_sorted_arrays.rb) | +| 10. Regular Expression Matching | [Link](https://leetcode.com/problems/regular-expression-matching/) | [Link](./lib/hard/10_regular_expression_matching.rb) | [Link](./test/hard/test_10_regular_expression_matching.rb) | +| 23. Merge k Sorted Lists | [Link](https://leetcode.com/problems/merge-k-sorted-lists/) | [Link](./lib/hard/23_merge_k_sorted_lists.rb) | [Link](./test/hard/test_23_merge_k_sorted_lists.rb) | +| 25. Reverse Nodes in k-Group | [Link](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Link](./lib/hard/25_reverse_nodes_in_k_group.rb) | [Link](./test/hard/test_25_reverse_nodes_in_k_group.rb) | +| 30. Substring with Concatenation of All Words | [Link](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [Link](./lib/hard/30_substring_with_concatenation_of_all_words.rb) | [Link](./test/hard/test_30_substring_with_concatenation_of_all_words.rb) | +| 32. Longest Valid Parentheses | [Link](https://leetcode.com/problems/longest-valid-parentheses/) | [Link](./lib/hard/32_longest_valid_parentheses.rb) | [Link](./test/hard/test_32_longest_valid_parentheses.rb) | +| 41. First Missing Positive | [Link](https://leetcode.com/problems/first-missing-positive/) | [Link](./lib/hard/41_first_missing_positive.rb) | [Link](./test/hard/test_41_first_missing_positive.rb) | +| 115. Distinct Subsequences | [Link](https://leetcode.com/problems/distinct-subsequences/) | [Link](./lib/hard/115_distinct_subsequences.rb) | [Link](./test/hard/test_115_distinct_subsequences.rb) | +| 126. Word Ladder II | [Link](https://leetcode.com/problems/word-ladder-ii/) | [Link](./lib/hard/126_word_ladder_ii.rb) | [Link](./test/hard/test_126_word_ladder_ii.rb) | +| 233. Number of Digit One | [Link](https://leetcode.com/problems/number-of-digit-one/) | [Link](./lib/hard/233_number_of_digit_one.rb) | [Link](./test/hard/test_233_number_of_digit_one.rb) | +| 239. Sliding Window Maximum | [Link](https://leetcode.com/problems/sliding-window-maximum/) | [Link](./lib/hard/239_sliding_window_maximum.rb) | [Link](./test/hard/test_239_sliding_window_maximum.rb) | +| 297. Serialize and Deserialize Binary Tree | [Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Link](./lib/hard/297_serialize_and_deserialize_binary_tree.rb) | [Link](./test/hard/test_297_serialize_and_deserialize_binary_tree.rb) | +| 336. Palindrome Pairs | [Link](https://leetcode.com/problems/palindrome-pairs/) | [Link](./lib/hard/336_palindrome_pairs.rb) | [Link](./test/hard/test_336_palindrome_pairs.rb) | +| 363. Max Sum of Rectangle No Larger Than K | [Link](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [Link](./lib/hard/363_max_sum_of_rectangle_no_larger_than_k.rb) | [Link](./test/hard/test_363_max_sum_of_rectangle_no_larger_than_k.rb) | +| 458. Poor Pigs | [Link](https://leetcode.com/problems/poor-pigs/) | [Link](./lib/hard/458_poor_pigs.rb) | [Link](./test/hard/test_458_poor_pigs.rb) | +| 871. Minimum Number of Refueling Stops | [Link](https://leetcode.com/problems/minimum-number-of-refueling-stops/) | [Link](./lib/hard/871_minimum_number_of_refueling_stops.rb) | [Link](./test/hard/test_871_minimum_number_of_refueling_stops.rb) | +| 895. Maximum Frequency Stack | [Link](https://leetcode.com/problems/maximum-frequency-stack/) | [Link](./lib/hard/895_maximum_frequency_stack.rb) | [Link](./test/hard/test_895_maximum_frequency_stack.rb) | +| 936. Stamping The Sequence | [Link](https://leetcode.com/problems/stamping-the-sequence/) | [Link](./lib/hard/936_stamping_the_sequence.rb) | [Link](./test/hard/test_936_stamping_the_sequence.rb) | +| 968. Binary Tree Cameras | [Link](https://leetcode.com/problems/binary-tree-cameras/) | [Link](./lib/hard/968_binary_tree_cameras.rb) | [Link](./test/hard/test_968_binary_tree_cameras.rb) | +| 1106. Parsing A Boolean Expression | [Link](https://leetcode.com/problems/parsing-a-boolean-expression/) | [Link](./lib/hard/1106_parsing_a_boolean_expression.rb) | [Link](./test/hard/test_1106_parsing_a_boolean_expression.rb) | +| 1220. Count Vowels Permutation | [Link](https://leetcode.com/problems/count-vowels-permutation/) | [Link](./lib/hard/1220_count_vowels_permutation.rb) | [Link](./test/hard/test_1220_count_vowels_permutation.rb) | +| 1770. Maximum Score from Performing Multiplication Operations | [Link](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/) | [Link](./lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb) | [Link](./test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 0eb4e120..a6a7d75b 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 = '8.0.4' + s.version = '8.0.5' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb b/lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb new file mode 100644 index 00000000..5eced7d4 --- /dev/null +++ b/lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: false + +# https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/ +# @param {Integer[]} nums +# @param {Integer[]} multipliers +# @return {Integer} +def maximum_score(nums, multipliers) + m = multipliers.length + dp = ::Array.new(m + 1) { ::Array.new(m + 1, 0) } + n = nums.length + + (m - 1).downto(0) do |op| + (op).downto(0) do |l| + dp[op][l] = [ + multipliers[op] * nums[l] + dp[op + 1][l + 1], + multipliers[op] * nums[n - 1 - (op - l)] + dp[op + 1][l] + ].max + end + end + + dp[0][0] +end diff --git a/test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb b/test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb new file mode 100644 index 00000000..deacda8e --- /dev/null +++ b/test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: false + +require_relative '../test_helper' +require_relative '../../lib/hard/1770_maximum_score_from_performing_multiplication_operations' +require 'minitest/autorun' + +class MaximumScoreFromPerformingMultiplicationOperationsTest < ::Minitest::Test + def test_default_one + assert_equal( + 14, + maximum_score( + [1, 2, 3], + [3, 2, 1] + ) + ) + end + + def test_default_two + assert_equal( + 102, + maximum_score( + [-5, -3, -3, -2, 7, 1], + [-10, -5, 3, 4, 6] + ) + ) + end +end