Skip to content

Commit f812ad5

Browse files
authored
2025-02-10 v. 8.4.8: added "1339. Maximum Product of Splitted Binary Tree"
2 parents daec02c + 807ee98 commit f812ad5

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
680680
| 1325. Delete Leaves With a Given Value | [Link](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Link](./lib/medium/1325_delete_leaves_with_a_given_value.rb) | [Link](./test/medium/test_1325_delete_leaves_with_a_given_value.rb) |
681681
| 1329. Sort the Matrix Diagonally | [Link](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [Link](./lib/medium/1329_sort_the_matrix_diagonally.rb) | [Link](./test/medium/test_1329_sort_the_matrix_diagonally.rb) |
682682
| 1338. Reduce Array Size to The Half | [Link](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Link](./lib/medium/1338_reduce_array_size_to_the_half.rb) | [Link](./test/medium/test_1338_reduce_array_size_to_the_half.rb) |
683+
| 1339. Maximum Product of Splitted Binary Tree | [Link](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Link](./lib/medium/1339_maximum_product_of_splitted_binary_tree.rb) | [Link](./test/medium/test_1339_maximum_product_of_splitted_binary_tree.rb) |
683684
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
684685
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
685686
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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 = '8.4.7'
8+
s.version = '8.4.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/
4+
# @param {TreeNode} root
5+
# @return {Integer}
6+
def max_product_for_splitted_binary_tree(root)
7+
@sum = sum_for_splitted_binary_tree(root)
8+
@result = 0
9+
10+
calculate_for_splitted_binary_tree(root)
11+
12+
(@result % (1e9 + 7)).to_i
13+
end
14+
15+
private
16+
17+
# @param {TreeNode}
18+
# @return {Integer}
19+
def sum_for_splitted_binary_tree(node)
20+
return 0 unless node
21+
22+
sum_for_splitted_binary_tree(node.left) + sum_for_splitted_binary_tree(node.right) + node.val
23+
end
24+
25+
# @param {TreeNode}
26+
# @return {Integer}
27+
def calculate_for_splitted_binary_tree(node)
28+
return 0 unless node
29+
30+
f_sum = calculate_for_splitted_binary_tree(node.left) + calculate_for_splitted_binary_tree(node.right) + node.val
31+
s_sum = @sum - f_sum
32+
33+
@result = [@result, f_sum * s_sum].max
34+
35+
f_sum
36+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/1339_maximum_product_of_splitted_binary_tree'
6+
require 'minitest/autorun'
7+
8+
class MaximumProductOfSplittedBinaryTreeTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
110,
12+
max_product_for_splitted_binary_tree(
13+
::TreeNode.build_tree(
14+
[1, 2, 3, 4, 5, 6]
15+
)
16+
)
17+
)
18+
end
19+
20+
def test_default_two
21+
assert_equal(
22+
90,
23+
max_product_for_splitted_binary_tree(
24+
::TreeNode.build_tree(
25+
[1, nil, 2, 3, 4, nil, nil, 5, 6]
26+
)
27+
)
28+
)
29+
end
30+
end

0 commit comments

Comments
 (0)