Time: O(n), Space: O(1)应该能作到. with Poster-Order process, recursively call on the left/right children, which returns: 1. The largest BST size in this branch 2. The largest BST size in this branch starting with the root 3. The value range [min, max] of the BST in (2) Based on this information returned from left/right children, the current root can easily figure out the things it needs to return itself.
M*e
16 楼
也有一大部分人在看球打嘴仗,无心学术
【在 l*******s 的大作中提到】 : 这题是不是难度太高了?
h*k
17 楼
recursion is never space O1, except tail recursion
【在 a***a 的大作中提到】 : Time: O(n), Space: O(1)应该能作到. : with Poster-Order process, recursively call on the left/right children, : which returns: : 1. The largest BST size in this branch : 2. The largest BST size in this branch starting with the root : 3. The value range [min, max] of the BST in (2) : Based on this information returned from left/right children, the current : root can easily figure out the things it needs to return itself.
证明部分留给你了 :) 不知道这个算不算一页 A_n = {n pairs of parentheses in the Catalan sense} B_n = {321-avoiding permutations in S_n} C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 , ()()(()) has c = 3. For any s in B_n, let m(s) be the number of positions one can insert n+1 to get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1. A_n C_n: take any t in A_n, reconstruct it according to the order of “) ”, from left to right. The corresponding element in C_n is the number of components c_i at each stage. For example, ()(()) is reconstructed as () -> ()() -> ()(()), and it has c_1 = 1, c_2 = c_3 = 2. It's not hard to see this is a bijection. B_n C_n: take any s in B_n, and check all the subsequences s_{(i)} made of {1,...,i} for any i. Let c_i be m(s_{(i)})-1. For example, 312 has subsequences 1, 12, 312, which maps to c_1 = 1, c_2 = c_3 = 2. It's a straightforward exercise to prove this is also a bijection.
【在 M*****e 的大作中提到】 : 证明部分留给你了 :) 不知道这个算不算一页 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} : For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 : , ()()(()) has c = 3. : For any s in B_n, let m(s) be the number of positions one can insert n+1 to : get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = : n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with : s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1.
l*i
34 楼
不明觉厉
2 to = with .
【在 M*****e 的大作中提到】 : 证明部分留给你了 :) 不知道这个算不算一页 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} : For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 : , ()()(()) has c = 3. : For any s in B_n, let m(s) be the number of positions one can insert n+1 to : get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = : n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with : s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1.
n*e
35 楼
继续拜读 往Catalan上凑
2 to = with .
【在 M*****e 的大作中提到】 : 证明部分留给你了 :) 不知道这个算不算一页 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} : For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 : , ()()(()) has c = 3. : For any s in B_n, let m(s) be the number of positions one can insert n+1 to : get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = : n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with : s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1.
p*e
36 楼
现了真身
2 to = with .
【在 M*****e 的大作中提到】 : 证明部分留给你了 :) 不知道这个算不算一页 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} : For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 : , ()()(()) has c = 3. : For any s in B_n, let m(s) be the number of positions one can insert n+1 to : get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = : n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with : s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1.
【在 M*****e 的大作中提到】 : 证明部分留给你了 :) 不知道这个算不算一页 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} : For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 : , ()()(()) has c = 3. : For any s in B_n, let m(s) be the number of positions one can insert n+1 to : get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = : n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with : s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1.
l*s
41 楼
c_i的定义可以简单点。 c_i=s_{(i)}的尾部单调递增的位数
2 to = with .
【在 M*****e 的大作中提到】 : 证明部分留给你了 :) 不知道这个算不算一页 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1} : For any t in A_n, let c(t) be the number of components. E.g., ()() has c = 2 : , ()()(()) has c = 3. : For any s in B_n, let m(s) be the number of positions one can insert n+1 to : get an element in B_{n+1}. To be more explicit, if s = 123..n, then m(s) = : n+1, otherwise there is a largest j(necessarily <= n-1) that has an i>j with : s_i < s_j, and in this case m(s) = n-j+1. One always has 2 <= m(s) <= n+1.
M*e
42 楼
对。靠,我可真大意,一篇帖子这么多笔误。
【在 l*******s 的大作中提到】 : 132的c_3=1? : : 2 : to : = : with : .
M*e
43 楼
恩。
【在 l*******s 的大作中提到】 : c_i的定义可以简单点。 : c_i=s_{(i)}的尾部单调递增的位数 : : 2 : to : = : with : .
为了你严谨的学术态度,我又写了一遍,但我语言组织能力差,看上去似乎更长了…… 主要是在C_n B_n 这段,为了和前面的可以无缝连接,我把它反过来写了,原帖 的方向描述更简单些。 A_n = {n pairs of parentheses in the Catalan sense} B_n = {321-avoiding permutations in S_n} C_n = {n-integer sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1 for i>= 2} A_n C_n: take any t in A_n, and reconstruct it according to the order of “)”, from left to right. The corresponding element in C_n is built by the number of components c_i at each stage. For example, ()(()) is reconstructed as () -> ()() -> ()(()), and it has component numbers c_1 = 1, c_2 = c_3 = 2. C_n B_n: for any c in C_n, one can construct an element in B_n as follows. First write down 1 for c_1 = 1, trivially in B_1. At stage i >= 2, suppose we already have a 321-avoiding permutation sequence, with the length of the last ascending run(*) c_{i-1}, we insert i into one of the c_{i-1} + 1 spaces the ascending run has, so that new last ascending run has length c _i. For any c_i in [1, c_{i-1} + 1], the choice exists and is unique. It's not hard to see the insertion keeps the sequence 321-free. For example, for c_1 = 1, c_2 = c_3 = 2, we get 1, 12, 312 as our permutation sequence. It's a straightforward exercise to prove both maps are bijective. *: An ascending run of a permutation is a nonempty increasing contiguous subsequence of the permutation that cannot be extended at either end. The last ascending run in this note is the ascending run that contains the last element of the sequence.
A_n = {n pairs of valid parentheses} B_n = {321-avoiding permutations in S_n} For any a_i in A_i, c(a_i) is defined as number of pairs of parentheses in a_i which are not within any other parentheses. For any a_n in A_n, (c(a_n,1),c(a_n,2),...c(a_n,n)) is called construction sequence of a_n, where a_n,i is sub-sequence of a_n made of of first i ")"s and their corresponding "("s. For example the construction sequence of ()(()) is (1,2,2) For any b_i in B_i, c(b_i) is defined as maximum number elements in increasing order at end of b_i. For any b_n in B_n, (c(b_n,1),c(b_n,2),...c(b_n,n)) is called construction sequence of b_n, where b_n,i is sub-sequence of b_n made of first i elements in S_n. For example the construction sequence of 312 is (1,2,2) It is clear that A_n and B_n are bijection by mapping elements in A_n to B_n with same construction sequence and vice versa.
>= of the
【在 M*****e 的大作中提到】 : 为了你严谨的学术态度,我又写了一遍,但我语言组织能力差,看上去似乎更长了…… : 主要是在C_n B_n 这段,为了和前面的可以无缝连接,我把它反过来写了,原帖 : 的方向描述更简单些。 : A_n = {n pairs of parentheses in the Catalan sense} : B_n = {321-avoiding permutations in S_n} : C_n = {n-integer sequence with c_1 = 1, and 1 <= c_i <= c_{i-1} + 1 for i>= : 2} : A_n C_n: take any t in A_n, and reconstruct it according to the order of : “)”, from left to right. The corresponding element in C_n is built by the : number of components c_i at each stage. For example, ()(()) is
M*e
47 楼
行。咱投哪里?
【在 l*******s 的大作中提到】 : A_n = {n pairs of valid parentheses} : B_n = {321-avoiding permutations in S_n} : For any a_i in A_i, c(a_i) is defined as number of pairs of : parentheses in a_i which are not within any other parentheses. : For any a_n in A_n, (c(a_n,1),c(a_n,2),...c(a_n,n)) is called : construction sequence of a_n, : where a_n,i is sub-sequence of a_n made of of first i ")"s : and their corresponding "("s. : For example the construction sequence of ()(()) is (1,2,2) : For any b_i in B_i, c(b_i) is defined as maximum number elements