Skip to content

Commit 3c22ecf

Browse files
committed
2025-02-25 v. 8.7.0: added "1530. Number of Good Leaf Nodes Pairs"
1 parent bba39d0 commit 3c22ecf

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
703703
| 1504. Count Submatrices With All Ones | [Link](https://leetcode.com/problems/count-submatrices-with-all-ones/) | [Link](./lib/medium/1504_count_submatrices_with_all_ones.rb) | [Link](./test/medium/test_1504_count_submatrices_with_all_ones.rb) |
704704
| 1525. Number of Good Ways to Split a String | [Link](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [Link](./lib/medium/1525_number_of_good_ways_to_split_a_string.rb) | [Link](./test/medium/test_1525_number_of_good_ways_to_split_a_string.rb) |
705705
| 1529. Minimum Suffix Flips | [Link](https://leetcode.com/problems/minimum-suffix-flips/) | [Link](./lib/medium/1529_minimum_suffix_flips.rb) | [Link](./test/medium/test_1529_minimum_suffix_flips.rb) |
706+
| 1530. Number of Good Leaf Nodes Pairs | [Link](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/) | [Link](./lib/medium/1530_number_of_good_leaf_nodes_pairs.rb) | [Link](./test/medium/test_1530_number_of_good_leaf_nodes_pairs.rb) |
706707
| 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) |
707708
| 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) |
708709
| 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) |

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.6.9'
8+
s.version = '8.7.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/
4+
# @param {TreeNode} root
5+
# @param {Integer} distance
6+
# @return {Integer}
7+
def count_good_pairs(root, distance)
8+
@result = 0
9+
calculate_for_count_pairs(root, distance)
10+
11+
@result
12+
end
13+
14+
private
15+
16+
# @param {TreeNode} root
17+
# @param {Integer} distance
18+
# @return {Integer[]}
19+
def calculate_for_count_pairs(node, distance)
20+
return [] unless node
21+
22+
return [1] if !node.left && !node.right
23+
24+
left = calculate_for_count_pairs(node.left, distance)
25+
right = calculate_for_count_pairs(node.right, distance)
26+
27+
left.each do |l|
28+
right.each do |r|
29+
@result += 1 if l + r <= distance
30+
end
31+
end
32+
33+
path = []
34+
35+
left.each { |l| path << l + 1 if l + 1 < distance }
36+
37+
right.each { |r| path << r + 1 if r + 1 < distance }
38+
39+
path
40+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/1530_number_of_good_leaf_nodes_pairs'
6+
require 'minitest/autorun'
7+
8+
class NumberOfGoodLeafNodesPairsTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
1,
12+
count_good_pairs(
13+
::TreeNode.build_tree(
14+
[1, 2, 3, nil, 4]
15+
),
16+
3
17+
)
18+
)
19+
end
20+
21+
def test_default_two
22+
assert_equal(
23+
2,
24+
count_good_pairs(
25+
::TreeNode.build_tree(
26+
[1, 2, 3, 4, 5, 6, 7]
27+
),
28+
3
29+
)
30+
)
31+
end
32+
33+
def test_default_three
34+
assert_equal(
35+
1,
36+
count_good_pairs(
37+
::TreeNode.build_tree(
38+
[7, 1, 4, 6, nil, 5, 3, nil, nil, nil, nil, nil, 2]
39+
),
40+
3
41+
)
42+
)
43+
end
44+
end

0 commit comments

Comments
 (0)