# LC 1569. Number of Ways to Reorder Array to Get Same BST ### [Problem link](https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/) ###### tags: `leedcode` `python` `hard` `Math` Given an array <code>nums</code> that represents a permutation of integers from <code>1</code> to <code>n</code>. We are going to construct a binary search tree (BST) by inserting the elements of <code>nums</code> in order into an initially empty BST. Find the number of different ways to reorder <code>nums</code> so that the constructed BST is identical to that formed from the original array <code>nums</code>. - For example, given <code>nums = [2,1,3]</code>, we will have 2 as the root, 1 as a left child, and 3 as a right child. The array <code>[2,3,1]</code> also yields the same BST but <code>[3,2,1]</code> yields a different BST. Return the number of ways to reorder <code>nums</code> such that the BST formed is identical to the original BST formed from <code>nums</code>. Since the answer may be very large, **return it modulo ** <code>10<sup>9</sup> + 7</code>. **Example 1:** <img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/bb.png" style="width: 121px; height: 101px;" /> ``` Input: nums = [2,1,3] Output: 1 Explanation: We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST. ``` **Example 2:** <img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/ex1.png" style="width: 241px; height: 161px;" /> ``` Input: nums = [3,4,5,1,2] Output: 5 Explanation: The following 5 arrays will yield the same BST: [3,1,2,4,5] [3,1,4,2,5] [3,1,4,5,2] [3,4,1,2,5] [3,4,1,5,2] ``` **Example 3:** <img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/ex4.png" style="width: 121px; height: 161px;" /> ``` Input: nums = [1,2,3] Output: 0 Explanation: There are no other orderings of nums that will yield the same BST. ``` **Constraints:** - <code>1 <= nums.length <= 1000</code> - <code>1 <= nums[i] <= nums.length</code> - All integers in <code>nums</code> are **distinct** . ## Solution 1 - Math ```python= class Solution: def numOfWays(self, nums: List[int]) -> int: mod = 10 ** 9 + 7 n = len(nums) combMemo = [[0 for _ in range(n)] for _ in range(n)] def calComb(m, n): ''' calculate C(m, n) ''' if combMemo[m][n] != 0: return combMemo[m][n] if n > m - n: return calComb(m, m - n) if n == 0: return 1 if n == 1: return m combMemo[m][n] = (calComb(m - 1, n) + calComb(m - 1, n - 1)) % mod return combMemo[m][n] def dfs(nums): m = len(nums) if m < 3: return 1 left_nodes, right_nodes = [], [] for num in nums[1:]: if num < nums[0]: left_nodes.append(num) else: right_nodes.append(num) left_val = dfs(left_nodes) right_val = dfs(right_nodes) return left_val * right_val * calComb(m - 1, len(left_nodes)) return (dfs(nums) - 1) % mod ``` >### Complexity >n = nums.length >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O($n^2$) | O($n^2$) | ## Note 計算combination時也可以用math.comb(), time using會差非常多. ![](https://hackmd.io/_uploads/BJmM96qv3.png) ![](https://hackmd.io/_uploads/H1sG569v2.png) [reference(video)](https://www.youtube.com/watch?v=FxGWaG9danM)