96. Unique Binary Search Trees
题目:
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
链接: http://leetcode.com/problems/unique-binary-search-trees/
题解:
用DP求catalan number。 Cn+1 = ∑(Cn - i * Ci ), i 的范围是( 0 ~ n)。公式不太好写,改天要用Latex编辑一下。
。有机会的话也要好好学习一下解析组合数学 - Analytic Combinatorics。Sedgewick有本书专门讲这个。
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public int numTrees(int n) { //catalan number
if(n <= 0)
return n;
int[] dp = new int[n + 1];
dp[0] = 1;
for(int i = 1; i < dp.length; i++) {
for(int j = 0; j < i; j++) {
dp[i] += dp[(i - 1) - j] *dp[j];
}
}
return dp[n];
}
}
或者用catalan数的另外一种推导,也是dp。
public class Solution {
public int numTrees(int n) { //Catalan number Cn+1 = 2(2n + 1)/ (n+2) * Cn
if(n < 0)
return 0;
int[] count = new int[n + 1];
count[0] = 1;
for(int i = 1; i < n + 1;i++)
count[i] = (int) (count[i - 1] * 2.0 *(2.0 *(i - 1) + 1.0) /(i - 1.0 + 2));
return count[n];
}
}
二刷:
求catalan number, 公式是Cn+1 = ∑(Cn - i * Ci ), 求和的范围是[0, n] 前后闭
Java:
public class Solution {
public int numTrees(int n) {
if (n <= 0) {
return n;
}
int[] dp = new int[n + 1];
dp[0] = 1;for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[(i - 1) - j] * dp[j];
}
}
return dp[n];
}
}
三刷:
这里要仔细注意一下dp数组的创建以及计算公式时的边界条件。 我们求第n个数的结果的话,其实是wiki公式里的第n + 1个数。所以我们建立一个长度为n + 1的一维数组dp,最后返回dp[n]就可以了。 其中Catalan number公式仍然用的是公式是Cn+1 = ∑(Cn - i * Ci ), 求和的范围是[0, n] 前后闭。所以假如我们要求dp[i], 那么内循环就是计算从0到 i-1 这 i 个数的乘积和。
Java:
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public int numTrees(int n) {
if (n <= 0) return 1;
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[(i - 1) - j];
}
}
return dp[n];
}
}
题外话:
有空的话还是要学一学离散数学的各种知识。
Reference:
http://www.codecogs.com/latex/eqneditor.php
http://en.wikipedia.org/wiki/Catalan_number
http://mathworld.wolfram.com/BinaryTree.html
96. Unique Binary Search Trees的更多相关文章
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- 96. Unique Binary Search Trees (Tree; DP)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode】96. Unique Binary Search Trees (2 solutions)
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 96. Unique Binary Search Trees(I 和 II)
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
随机推荐
- Codevs 2296 仪仗队 2008年省队选拔赛山东
2296 仪仗队 2008年省队选拔赛山东 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 作为体育委员,C君负责这次运动 ...
- 2014-10 u-boot 顶层config.mk分析
/** ****************************************************************************** * @author Maox ...
- 打破常规——大胆尝试在路由器上搭建SVN服务器
注册博客园挺久了,一直比较懒,虽然有几次想写点文章,但是一直没有行动,今天给大家带来一篇比较有意思的文章,不涉及技术上的,希望大家轻拍.本文的文字和图片全部为原创,尊重作者转载请注明出处! 说起路由器 ...
- Hibernate一对一双向关联映射
关键原因在于对象模型具有方向性: 单向:一端只能加载另一端,不能反过来. 双向:两端都可以加载另一端. 问题来了:如何我们想从身份证端(IdCard)加载人(Person),怎么办呢? 下面我们开始介 ...
- JavaScript技巧45招
原文:45 Useful JavaScript Tips, Tricks and Best Practices作者:Saad Mousliki 在这篇文章里,我将分享一些JavaScript的技巧.秘 ...
- 解决未能从程序集xxx中加载类型System.ServiceModel.Activation.HttpModule的问题
在IIS中运行网站时,出现错误: 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c ...
- pdf增加水印
/// <summary> /// 为PDF添加水印或背景图片 /// </summary> /// <param name="strSourceFilePat ...
- easy ui 下拉级联效果 ,下拉框绑定数据select控件
html代码: ①两个下拉框,一个是省,另一个市 <tr> <td>省:</td> <td> <select id="ProvinceI ...
- POJ 2151 概率DP
主要的子问题是每一个队伍有一个做出题目的概率,求做出k个题目的概率.简单的简单的组合数DP.想清楚即可. 1: #include <iostream> 2: #include <cs ...
- 【BZOJ 1834】 [ZJOI2010]network 网络扩容
Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的 ...