From 69c27a00676079b045fc067b7b3b530fda2b3f9e Mon Sep 17 00:00:00 2001 From: fartem Date: Thu, 2 Jan 2025 11:23:18 +0300 Subject: [PATCH] 2025-01-02 v. 7.6.2: updated "823. Binary Trees With Factors" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/823_binary_trees_with_factors.rb | 26 +++++++++++++++++++ .../test_823_binary_trees_with_factors.rb | 25 ++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/medium/823_binary_trees_with_factors.rb create mode 100644 test/medium/test_823_binary_trees_with_factors.rb diff --git a/README.md b/README.md index e2d8684b..4b229f95 100644 --- a/README.md +++ b/README.md @@ -624,6 +624,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 797. All Paths From Source to Target | [Link](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Link](./lib/medium/797_all_paths_from_source_to_target.rb) | [Link](./test/medium/test_797_all_paths_from_source_to_target.rb) | | 814. Binary Tree Pruning | [Link](https://leetcode.com/problems/binary-tree-pruning/) | [Link](./lib/medium/814_binary_tree_pruning.rb) | [Link](./test/medium/test_814_binary_tree_pruning.rb) | | 817. Linked List Components | [Link](https://leetcode.com/problems/linked-list-components/) | [Link](./lib/medium/817_linked_list_components.rb) | [Link](./test/medium/test_817_linked_list_components.rb) | +| 823. Binary Trees With Factors | [Link](https://leetcode.com/problems/binary-trees-with-factors/) | [Link](./lib/medium/823_binary_trees_with_factors.rb) | [Link](./test/medium/test_823_binary_trees_with_factors.rb) | ### Hard diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 1cefc4e9..982b44da 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 = '7.6.1' + s.version = '7.6.2' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/823_binary_trees_with_factors.rb b/lib/medium/823_binary_trees_with_factors.rb new file mode 100644 index 00000000..6c2d4b2b --- /dev/null +++ b/lib/medium/823_binary_trees_with_factors.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/binary-trees-with-factors/ +# @param {Integer[]} arr +# @return {Integer} +def num_factored_binary_trees(arr) + mod = 1_000_000_007 + arr.sort! + dp = ::Array.new(arr.size, 1) + index = {} + arr.each_with_index { |val, i| index[val] = i } + (0...arr.size).each do |i| + (0...i).each do |j| + next unless (arr[i] % arr[j]).zero? + + r = arr[i] / arr[j] + + dp[i] = (dp[i] + dp[j] * dp[index[r]]) % mod if index.key?(r) + end + end + + result = 0 + dp.each { |val| result = (result + val) % mod } + + result +end diff --git a/test/medium/test_823_binary_trees_with_factors.rb b/test/medium/test_823_binary_trees_with_factors.rb new file mode 100644 index 00000000..afeeb329 --- /dev/null +++ b/test/medium/test_823_binary_trees_with_factors.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/823_binary_trees_with_factors' +require 'minitest/autorun' + +class BinaryTreesWithFactorsTest < ::Minitest::Test + def test_default_one + assert_equal( + 3, + num_factored_binary_trees( + [2, 4] + ) + ) + end + + def test_default_two + assert_equal( + 7, + num_factored_binary_trees( + [2, 4, 5, 10] + ) + ) + end +end