File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff 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 ) |
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments