{"id":7320,"date":"2020-08-30T01:48:13","date_gmt":"2020-08-30T08:48:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7320"},"modified":"2020-08-30T12:27:45","modified_gmt":"2020-08-30T19:27:45","slug":"leetcode-1569-number-of-ways-to-reorder-array-to-get-same-bst","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/recursion\/leetcode-1569-number-of-ways-to-reorder-array-to-get-same-bst\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1569. Number of Ways to Reorder Array to Get Same BST"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;that represents a permutation of integers from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. We are going to construct a binary search tree (BST) by inserting the elements of&nbsp;<code>nums<\/code>&nbsp;in&nbsp;order into an initially empty BST. Find the number of different ways to reorder&nbsp;<code>nums<\/code>&nbsp;so that the constructed BST is identical to that formed from the original array&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>For example, given&nbsp;<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&nbsp;<code>[2,3,1]<\/code>&nbsp;also yields the same BST but&nbsp;<code>[3,2,1]<\/code>&nbsp;yields a different BST.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of ways to reorder<\/em>&nbsp;<code>nums<\/code>&nbsp;<em>such that the BST formed is identical to the original BST formed from<\/em>&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>Since the answer may be very large,&nbsp;<strong>return it modulo&nbsp;<\/strong><code>10^9 + 7<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/12\/bb.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,1,3]\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>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.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/12\/ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,4,5,1,2]\n<strong>Output:<\/strong> 5\n<strong>Explanation: <\/strong>The following 5 arrays will yield the same BST: \n[3,1,2,4,5]\n[3,1,4,2,5]\n[3,1,4,5,2]\n[3,4,1,2,5]\n[3,4,1,5,2]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/12\/ex4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3]\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>There are no other orderings of nums that will yield the same BST.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/12\/abc.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,1,2,5,4,6]\n<strong>Output:<\/strong> 19\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [9,4,2,1,3,6,5,7,8,14,11,10,12,13,16,15,17,18]\n<strong>Output:<\/strong> 216212978\n<strong>Explanation: <\/strong>The number of ways to reorder nums to get the same BST is 3216212999. Taking this number modulo 10^9 + 7 gives 216212978.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= nums.length<\/code><\/li><li>All integers in&nbsp;<code>nums<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion + Combinatorics<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1569-ep353.png\" alt=\"\" class=\"wp-image-7324\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1569-ep353.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1569-ep353-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1569-ep353-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>For a given root (first element of the array), we can split the array into left children (nums[i] &lt; nums[0]) and right children (nums[i] &gt; nums[0]). Assuming there are l nodes for the left and r nodes for the right. We have C(l + r, l) different ways to insert l elements into a (l + r) sized array. Within node l \/ r nodes, we have ways(left) \/ ways(right) different ways to re-arrange those nodes. So the total # of ways is:<br>C(l + r, l) * ways(l) * ways(r)<br>Don&#8217;t forget to minus one for the final answer.<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int numOfWays(vector<int>& nums) {\n    const int n = nums.size();\n    const int kMod = 1e9 + 7;\n    vector<vector<int>> cnk(n + 1, vector<int>(n + 1, 1));    \n    for (int i = 1; i <= n; ++i)      \n      for (int j = 1; j < i; ++j)\n        cnk[i][j] = (cnk[i - 1][j] + cnk[i - 1][j - 1]) % kMod;    \n    function<int(vector<int>)> trees = [&](const vector<int>& nums) {\n      const int n = nums.size();\n      if (n <= 2) return 1;\n      vector<int> left;\n      vector<int> right;\n      for (int i = 1; i < nums.size(); ++i)\n        if (nums[i] < nums[0]) left.push_back(nums[i]);\n        else right.push_back(nums[i]);\n      long ans = cnk[n - 1][left.size()];\n      ans = (ans * trees(left)) % kMod;      \n      ans = (ans * trees(right)) % kMod;      \n      return static_cast<int>(ans);\n    };\n    return trees(nums) - 1;\n  } \n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python3\">\nclass Solution:\n  def numOfWays(self, nums: List[int]) -> int:\n    def ways(nums):\n      if len(nums) <= 2: return 1\n      l = [x for x in nums if x < nums[0]]\n      r = [x for x in nums if x > nums[0]]\n      return comb(len(l) + len(r), len(l)) * ways(l) * ways(r)\n    return (ways(nums) - 1) % (10**9 + 7)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;that represents a permutation of integers from&nbsp;1&nbsp;to&nbsp;n. We are going to construct a binary search tree (BST) by inserting the elements of&nbsp;nums&nbsp;in&nbsp;order into&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[122,217,31,17],"class_list":["post-7320","post","type-post","status-publish","format-standard","hentry","category-recursion","tag-combination","tag-hard","tag-math","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7320"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7320\/revisions"}],"predecessor-version":[{"id":7325,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7320\/revisions\/7325"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}