当前位置:网站首页>[dynamic programming] different binary search trees
[dynamic programming] different binary search trees
2022-04-23 07:17:00 【Breeze_】
Give you an integer
n, Seek just bynComposed of nodes with node values from1TonDifferent from each other Binary search tree How many kinds? ? Returns the number of binary search trees satisfying the meaning of the question .link :https://leetcode-cn.com/problems/unique-binary-search-trees/
Definition : d p [ i ] dp[i] dp[i] Indicates that the slave node is i i i The number of different binary search trees
The boundary conditions : d p [ 0 ] = 1 , d p [ 1 ] = 1 , d p [ 2 ] = 2 dp[0]=1,dp[1]=1,dp[2]=2 dp[0]=1,dp[1]=1,dp[2]=2, No nodes , The empty set is unique ;1 Nodes , A kind of ;2 Nodes , Two kinds of
State transition equation : d p [ i ] = ∑ j = 0 i − 1 d p [ j ] ∗ d p [ i − j − 1 ] dp[i] = \sum\limits_{j=0}^{i-1} dp[j]* dp[i-j-1] dp[i]=j=0∑i−1dp[j]∗dp[i−j−1]
explain :
When there are no nodes , The empty set is unique ;
1 Nodes , A kind of ;
2 Nodes , Two kinds of , Namely , Given a node , The left subtree 1 Nodes , Right subtree 0 Nodes , And given a node , The left subtree 0 Nodes , Right subtree 1 Nodes
3 Nodes ,5 Kind of , Namely , Given a node ,① The left subtree 2 Nodes , Right subtree 0 Nodes ;② The left subtree 1 Nodes , Right subtree 1 Nodes ;③ The left subtree 0 Nodes , Right subtree 2 Nodes . For the situation 1 And circumstances 3, They can be expressed as d p [ 2 ] ∗ d p [ 0 ] dp[2]*dp[0] dp[2]∗dp[0] and d p [ 0 ] ∗ d p [ 2 ] dp[0]*dp[2] dp[0]∗dp[2] The situation of , Others in the same way , That is, the corresponding state transition equation .
class Solution:
def numTrees(self, n: int) -> int:
# Dynamic programming method
#dp[i] = Σdp[k]*dp[i-k-1]
dp = [0]*(n+1)
dp[0] = 1 # 0 Elements
dp[1] = 1 # 1 Elements
if n<=1: return dp[n]
for i in range(2,n+1):
for j in range(i):
dp[i] += dp[i-j-1]*dp[j]
return dp[n]
The complexity of this method is O(n^2), You can use mathematical methods , Get the general term formula ( Carter LAN number ), Reduce complexity
版权声明
本文为[Breeze_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323347.html
边栏推荐
猜你喜欢

常用UI控件简写名

Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation

组件化学习(3)ARouter中的Path和Group注解

Binder mechanism principle

基于BottomNavigationView实现底部导航栏

Cancel remote dependency and use local dependency

Itop4412 HDMI display (4.4.4_r1)

Personal blog website construction

ThreadLocal,看我就够了!

Binder机制原理
随机推荐
PyTorch 模型剪枝实例教程三、多参数与全局剪枝
给女朋友写个微信双开小工具
face_recognition人脸检测
[2021 book recommendation] kubernetes in production best practices
组件化学习(3)ARouter中的Path和Group注解
机器学习笔记 一:学习思路
xcode 编译速度慢的解决办法
iTOP4412 HDMI显示(4.4.4_r1)
Bottom navigation bar based on bottomnavigationview
Personal blog website construction
[SM8150][Pixel4]LCD驱动
Google AdMob advertising learning
BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
[recommendation for new books in 2021] professional azure SQL managed database administration
Binder mechanism principle
[2021 book recommendation] learn winui 3.0
WebView displays a blank due to a certificate problem
iTOP4412 SurfaceFlinger(4.0.3_r1)
Component based learning (3) path and group annotations in arouter
MySQL notes 2_ data sheet