Skip to content

Commit 1f4bdc5

Browse files
committed
1 parent 6d3ae54 commit 1f4bdc5

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,4 @@ mod s1563_stone_game_v;
11831183
mod s1566_detect_pattern_of_length_m_repeated_k_or_more_times;
11841184
mod s1567_maximum_length_of_subarray_with_positive_product;
11851185
mod s1568_minimum_number_of_days_to_disconnect_island;
1186+
mod s1569_number_of_ways_to_reorder_array_to_get_same_bst;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* [1569] Number of Ways to Reorder Array to Get Same BST
3+
*
4+
* Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
5+
*
6+
* For example, given nums = [2,1,3], we will have 2 as the root, 1 as a left child, and 3 as a right child. The array [2,3,1] also yields the same BST but [3,2,1] yields a different BST.
7+
*
8+
* Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
9+
* Since the answer may be very large, return it modulo 10^9 + 7.
10+
*
11+
* Example 1:
12+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/bb.png" style="width: 121px; height: 101px;" />
13+
* Input: nums = [2,1,3]
14+
* Output: 1
15+
* 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.
16+
*
17+
* Example 2:
18+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/ex1.png" style="width: 241px; height: 161px;" />
19+
* Input: nums = [3,4,5,1,2]
20+
* Output: 5
21+
* Explanation: The following 5 arrays will yield the same BST:
22+
* [3,1,2,4,5]
23+
* [3,1,4,2,5]
24+
* [3,1,4,5,2]
25+
* [3,4,1,2,5]
26+
* [3,4,1,5,2]
27+
*
28+
* Example 3:
29+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/ex4.png" style="width: 121px; height: 161px;" />
30+
* Input: nums = [1,2,3]
31+
* Output: 0
32+
* Explanation: There are no other orderings of nums that will yield the same BST.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= nums.length <= 1000
38+
* 1 <= nums[i] <= nums.length
39+
* All integers in nums are distinct.
40+
*
41+
*/
42+
pub struct Solution {}
43+
44+
// problem: https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/
45+
// discuss: https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/discuss/?currentPage=1&orderBy=most_votes&query=
46+
47+
// submission codes start here
48+
49+
impl Solution {
50+
// Credit: https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/solutions/3643480/rust-dfs-solution/
51+
52+
const MOD: i32 = 1_000_000_007;
53+
54+
pub fn num_of_ways(nums: Vec<i32>) -> i32 {
55+
let n = nums.len();
56+
let mut cnk = vec![vec![1; n + 1]; n + 1];
57+
58+
for i in 1..n + 1 {
59+
for j in 1..i {
60+
cnk[i][j] = (cnk[i - 1][j] + cnk[i - 1][j - 1]) % Self::MOD;
61+
}
62+
}
63+
64+
Self::dfs_helper(nums, &cnk) - 1
65+
}
66+
67+
fn dfs_helper(nums: Vec<i32>, cnk: &Vec<Vec<i32>>) -> i32 {
68+
let n = nums.len();
69+
70+
if n <= 2 {
71+
return 1;
72+
}
73+
74+
let (mut left, mut right) = (vec![], vec![]);
75+
76+
for i in 1..n {
77+
if nums[i] < nums[0] {
78+
left.push(nums[i]);
79+
} else {
80+
right.push(nums[i]);
81+
}
82+
}
83+
84+
let mut result = cnk[n - 1][left.len()] as i64;
85+
result = (result * Self::dfs_helper(left, cnk) as i64) % Self::MOD as i64;
86+
result = (result * Self::dfs_helper(right, cnk) as i64) % Self::MOD as i64;
87+
88+
result as i32
89+
}
90+
}
91+
92+
// submission codes end
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
98+
#[test]
99+
fn test_1569_example_1() {
100+
let nums = vec![2, 1, 3];
101+
102+
let result = 1;
103+
104+
assert_eq!(Solution::num_of_ways(nums), result);
105+
}
106+
107+
#[test]
108+
fn test_1569_example_2() {
109+
let nums = vec![3, 4, 5, 1, 2];
110+
111+
let result = 5;
112+
113+
assert_eq!(Solution::num_of_ways(nums), result);
114+
}
115+
116+
#[test]
117+
fn test_1569_example_3() {
118+
let nums = vec![1, 2, 3];
119+
120+
let result = 0;
121+
122+
assert_eq!(Solution::num_of_ways(nums), result);
123+
}
124+
}

0 commit comments

Comments
 (0)