Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '8.6.9'
s.version = '8.7.0'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
40 changes: 40 additions & 0 deletions lib/medium/1530_number_of_good_leaf_nodes_pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/
# @param {TreeNode} root
# @param {Integer} distance
# @return {Integer}
def count_good_pairs(root, distance)
@result = 0
calculate_for_count_pairs(root, distance)

@result
end

private

# @param {TreeNode} root
# @param {Integer} distance
# @return {Integer[]}
def calculate_for_count_pairs(node, distance)
return [] unless node

return [1] if !node.left && !node.right

left = calculate_for_count_pairs(node.left, distance)
right = calculate_for_count_pairs(node.right, distance)

left.each do |l|
right.each do |r|
@result += 1 if l + r <= distance
end
end

path = []

left.each { |l| path << l + 1 if l + 1 < distance }

right.each { |r| path << r + 1 if r + 1 < distance }

path
end
44 changes: 44 additions & 0 deletions test/medium/test_1530_number_of_good_leaf_nodes_pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/binary_tree'
require_relative '../../lib/medium/1530_number_of_good_leaf_nodes_pairs'
require 'minitest/autorun'

class NumberOfGoodLeafNodesPairsTest < ::Minitest::Test
def test_default_one
assert_equal(
1,
count_good_pairs(
::TreeNode.build_tree(
[1, 2, 3, nil, 4]
),
3
)
)
end

def test_default_two
assert_equal(
2,
count_good_pairs(
::TreeNode.build_tree(
[1, 2, 3, 4, 5, 6, 7]
),
3
)
)
end

def test_default_three
assert_equal(
1,
count_good_pairs(
::TreeNode.build_tree(
[7, 1, 4, 6, nil, 5, 3, nil, nil, nil, nil, nil, 2]
),
3
)
)
end
end