Skip to content

Commit 2aed7d9

Browse files
committed
add unique_bsts problem
1 parent 5d39d10 commit 2aed7d9

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This is my way of saying - change your interview style. There are lots of smart
1313

1414
| problem | solution |
1515
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
16+
| [Unique binary search trees](https://leetcode.com/problems/unique-binary-search-trees/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/house-robber.rb) |
1617
| [House Robber](https://leetcode.com/problems/house-robber/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/house-robber.rb) |
1718
| [Array Product](https://leetcode.com/problems/product-of-array-except-self/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/product-of-array.rb) |
1819
| [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/top-k-elements.rb) |

src/unique_bsts.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def unique_bsts(n)
2+
a = Array.new(n+1, 0)
3+
a[0] = a[1] = 1
4+
5+
(2..n).each do |i|
6+
(1..i).each do |j|
7+
a[i] += (a[j-1] * a[i-j])
8+
end
9+
end
10+
11+
return a[n]
12+
end
13+
14+
p unique_bsts(4) # => 14

0 commit comments

Comments
 (0)