Skip to content

Commit 7c7180f

Browse files
authored
Merge pull request #691 from fartem/96_Unique_Binary_Search_Trees
2024-07-29 v. 6.3.3: added "96. Unique Binary Search Trees"
2 parents dbf6630 + 20e0dee commit 7c7180f

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
498498
| 86. Partition List | [Link](https://leetcode.com/problems/partition-list/) | [Link](./lib/medium/86_partition_list.rb) |
499499
| 92. Reverse Linked List II | [Link](https://leetcode.com/problems/reverse-linked-list-ii/) | [Link](./lib/medium/92_reverse_linked_list_ii.rb) |
500500
| 95. Unique Binary Search Trees II | [Link](https://leetcode.com/problems/unique-binary-search-trees-ii/) | [Link](./lib/medium/95_unique_binary_search_trees_ii.rb) |
501+
| 96. Unique Binary Search Trees | [Link](https://leetcode.com/problems/unique-binary-search-trees/) | [Link](./lib/medium/96_unique_binary_search_trees.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 = '6.3.2'
8+
s.version = '6.3.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/unique-binary-search-trees/
4+
# @param {Integer} n
5+
# @return {Integer}
6+
def num_trees(n)
7+
arr = ::Array.new(n + 2, 0)
8+
arr[0] = arr[1] = 1
9+
10+
(2..n).each do |i|
11+
(0...i).each do |j|
12+
arr[i] += arr[j] * arr[i - j - 1]
13+
end
14+
end
15+
16+
arr[n]
17+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/96_unique_binary_search_trees'
6+
require 'minitest/autorun'
7+
8+
class UniqueBinarySearchTreesTest < ::Minitest::Test
9+
def test_default
10+
assert_equal(5, num_trees(3))
11+
assert_equal(1, num_trees(1))
12+
end
13+
end

0 commit comments

Comments
 (0)