question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-the-number-of-good-pairs-i | Super easy Python Solution| Beginner friendly | super-easy-python-solution-beginner-frie-pr6x | Code | rajsekhar5161 | NORMAL | 2025-03-28T15:30:59.786786+00:00 | 2025-03-28T15:30:59.786786+00:00 | 3 | false |
# Code
```python []
class Solution(object):
def numberOfPairs(self, num1, num2, k):
count=0
for i in range(len(num1)):
for j in range(len(num2)):
if num1[i]%(num2[j]*k)==0:
count+=1
return count
``` | 0 | 0 | ['Array', 'Hash Table', 'Python'] | 0 |
find-the-number-of-good-pairs-i | Divisible Detectives 🕵️♂️🔢 – Finding Good Pairs! | divisible-detectives-finding-good-pairs-6sqev | IntuitionThe problem requires us to find pairs ((i, j)) where the element from nums1 is divisible by the product of an element from nums2 and k. This suggests i | Shahin1212 | NORMAL | 2025-03-25T06:06:54.090839+00:00 | 2025-03-25T06:06:54.090839+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to find pairs \((i, j)\) where the element from `nums1` is divisible by the product of an element from `nums2` and `k`. This suggests iterating through all possible pairs and checking the divisibility condition.
# A... | 0 | 0 | ['Python3'] | 0 |
find-the-number-of-good-pairs-i | Simple solution | simple-solution-by-mrwan54-rfww | Code | mrwan54 | NORMAL | 2025-03-24T22:26:19.306409+00:00 | 2025-03-24T22:26:19.306409+00:00 | 1 | false |
# Code
```cpp []
class Solution {
public:
int numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
int res = 0;
size_t len1 = nums1.size(), len2 = nums2.size();
for (size_t i = 0; i < len1; ++i)
{
for (size_t j = 0; j < len2; ++j)
{
... | 0 | 0 | ['C++'] | 0 |
find-the-number-of-good-pairs-i | ✅ [Python] USING HASHMAP, TC O(n + m) ✅ | python-using-hashmap-tc-on-m-by-rk_dp-4gfb | 1. Brute ForceTIme ComplexityO(n * M)Space ComplexityO(1)2. HashMapTIme ComplexityO(n + M)Space ComplexityO(1)Code | rk_dp | NORMAL | 2025-03-22T08:29:13.267993+00:00 | 2025-03-22T08:29:13.267993+00:00 | 2 | false | # **1. Brute Force**
# **TIme Complexity**
O(n * M)
# **Space Complexity**
O(1)
```
count = 0
for i in range(len(nums1)):
for j in range(len(nums2)):
if nums1[i] % (nums2[j] * k) == 0:
count += 1
return count
```
# **2. HashMap**
# **TIme Complexity**
O(n + M)
# **Space Complexity**
... | 0 | 0 | ['Array', 'Hash Table', 'Python3'] | 0 |
find-the-number-of-good-pairs-i | 🚀Easy C Language Solution Using Suitable Array Concepts🚀 | easy-c-language-solution-using-suitable-t4qoa | IntuitionThe function calculates the number of pairs (nums1[i], nums2[j]) where the element from nums1 is divisible by k times the element from nums2. This invo | Vivek_Bartwal | NORMAL | 2025-03-22T06:59:35.389699+00:00 | 2025-03-22T06:59:35.389699+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The function calculates the number of pairs (nums1[i], nums2[j]) where the element from nums1 is divisible by k times the element from nums2. This involves checking each pair of elements from both arrays and applying the divisibility condit... | 0 | 0 | ['Array', 'Hash Table', 'C'] | 0 |
find-the-number-of-good-pairs-i | 🔢 Count Valid Pairs | count-valid-pairs-by-akhildas675-ujrd | IntuitionThe problem requires us to find the number of valid pairs (i, j) such that:
nums1[i]mod (nums2[j]×k)==0
nums1[i]mod(nums2[j]×k)==0My first thought was | akhildas675 | NORMAL | 2025-03-22T05:10:43.551660+00:00 | 2025-03-22T05:10:43.551660+00:00 | 3 | false | # Intuition
The problem requires us to find the number of valid pairs (i, j) such that:
nums1[i]mod (nums2[j]×k)==0
nums1[i]mod(nums2[j]×k)==0
My first thought was to use a brute force approach: iterate through all possible (i, j) pairs and check the condition.
# Approach
1. Initialize count = 0 to store the number ... | 0 | 0 | ['JavaScript'] | 0 |
find-the-number-of-good-pairs-i | Easy Java Code | easy-java-code-by-sindhumandadapu24-wa2w | class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int c=0;
for(int i=0;i<nums1.length;i++){
for(int j=0;j<nums2.length;j++){
if((nums | sindhumandadapu24 | NORMAL | 2025-03-18T08:43:58.222479+00:00 | 2025-03-18T08:43:58.222479+00:00 | 1 | false | class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int c=0;
for(int i=0;i<nums1.length;i++){
for(int j=0;j<nums2.length;j++){
if((nums1[i]%(nums2[j]*k)==0)){
c++;
}
}
}
return c;
}
} | 0 | 0 | ['Java'] | 0 |
find-the-number-of-good-pairs-i | LIST OF C# SOLUTION FOR SMALL AND LARGE ARRAYS (3 C# Solutions) | list-of-c-solution-for-small-and-large-a-ws7m | Intuition / Approach / ComplexityThis is an easy question, we can have a solution without using dictionary, just a brute force solution, we can iterate all nums | RachidBelouche | NORMAL | 2025-03-14T11:35:38.652048+00:00 | 2025-03-14T11:35:38.652048+00:00 | 4 | false | # Intuition / Approach / Complexity
<!-- Describe your first thoughts on how to solve this problem. -->
This is an easy question, we can have a solution without using dictionary, just a brute force solution, we can iterate all nums2 and change the number in nums2 to number * k (this will save time iterating all nums1 n... | 0 | 0 | ['Array', 'Hash Table', 'Counting', 'Number Theory', 'C#'] | 0 |
find-the-number-of-good-pairs-i | Simple solution in Java. Beats 100 % | simple-solution-in-java-beats-100-by-kha-qb6o | Complexity
Time complexity:
O(m * n)
Space complexity:
O(1)
Code | Khamdam | NORMAL | 2025-03-13T09:41:41.541160+00:00 | 2025-03-13T09:41:41.541160+00:00 | 2 | false | # Complexity
- Time complexity:
O(m * n)
- Space complexity:
O(1)
# Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int goodPairs = 0;
int n = nums1.length;
int m = nums2.length;
for (int i = 0; i < n; i++) {
for (int j = 0; ... | 0 | 0 | ['Array', 'Java'] | 0 |
find-the-number-of-good-pairs-i | Simple solution - beats 100%🔥 | simple-solution-beats-100-by-cyrusjetson-z01i | Complexity
Time complexity: O(N * N)
Space complexity: O(N)
Code | cyrusjetson | NORMAL | 2025-03-13T09:04:52.551458+00:00 | 2025-03-13T09:04:52.551458+00:00 | 1 | false | # Complexity
- Time complexity: O(N * N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int count = 0;
for (int i = ... | 0 | 0 | ['Java'] | 0 |
find-the-number-of-good-pairs-i | Easy Java Solution | easy-java-solution-by-prachijain-9p63 | IntuitionApproachComplexity
Time complexity:
O(n*m)
Space complexity:
O(1)
Code | prachijain | NORMAL | 2025-03-08T14:37:59.032503+00:00 | 2025-03-08T14:37:59.032503+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(n*m)
- Space complexity:
O(1)
# Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
... | 0 | 0 | ['Java'] | 0 |
find-the-number-of-good-pairs-i | Simple solution - beats 100% 🔥 | simple-solution-beats-100-by-joshuaimman-vl1p | Complexity
Time complexity : O(N * M)
Space complexity: O(1)
Code | joshuaimmanuelin | NORMAL | 2025-03-08T07:36:54.653338+00:00 | 2025-03-08T07:36:54.653338+00:00 | 1 | false |
# Complexity
- Time complexity : O(N * M)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int goodPair = 0;
for (in... | 0 | 0 | ['Java'] | 0 |
find-the-number-of-good-pairs-i | Very Easy and Simple | very-easy-and-simple-by-yashu__007-ci2o | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Yashu__007 | NORMAL | 2025-03-08T06:44:57.975055+00:00 | 2025-03-08T06:44:57.975055+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
find-the-number-of-good-pairs-i | test_from_immortal | test_from_immortal-by-immortal_039-4eph | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Immortal_039 | NORMAL | 2025-03-07T20:32:32.015342+00:00 | 2025-03-07T20:32:32.015342+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
find-the-number-of-good-pairs-i | Easy || JavaScript | easy-javascript-by-mohdaman7-qs94 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Mohdaman7 | NORMAL | 2025-03-06T06:55:21.132536+00:00 | 2025-03-06T06:55:21.132536+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['JavaScript'] | 0 |
find-the-number-of-good-pairs-i | Easy-Javascript!!! | easy-javascript-by-nishanaaaah-gkzo | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | nishanaaaah | NORMAL | 2025-03-06T05:36:23.741817+00:00 | 2025-03-06T05:36:23.741817+00:00 | 5 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['JavaScript'] | 0 |
find-the-number-of-good-pairs-i | My submission beat 100% of other submissions' runtime. | my-submission-beat-100-of-other-submissi-l70m | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | limon4ik13 | NORMAL | 2025-03-05T11:52:40.697184+00:00 | 2025-03-05T11:52:40.697184+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Hash Table', 'C++'] | 0 |
find-the-number-of-good-pairs-i | Simple Java Solution Beats 100% | simple-java-solution-beats-100-by-sairaj-r1k9 | Code | Sairaj_Thakar | NORMAL | 2025-02-24T15:28:51.946116+00:00 | 2025-02-24T15:28:51.946116+00:00 | 3 | false | # Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int res = 0;
for(int i = 0; i<nums1.length; i++)
{
int out= nums1[i];
for(int j =0; j<nums2.length; j++)
{
int inn = nums2[j];
... | 0 | 0 | ['Java'] | 0 |
find-the-number-of-good-pairs-i | Using for loop | using-for-loop-by-vijayakumar-1728-dhxc | Code | vijayakumar-1728 | NORMAL | 2025-02-22T15:02:57.697827+00:00 | 2025-02-22T15:02:57.697827+00:00 | 1 | false |
# Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int n=nums1.length;
int m=nums2.length;
int c=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(nums1[i]%(nums2[j]*k)==0){
c++;
... | 0 | 0 | ['Java'] | 0 |
find-the-number-of-good-pairs-i | Brute Force | brute-force-by-meky20500-ywva | IntuitionApproachComplexity
Time complexity: O(n^2)
Space complexity:O(1)
Code | meky20500 | NORMAL | 2025-02-19T20:26:39.504336+00:00 | 2025-02-19T20:26:39.504336+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(n^2)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Array', 'C#'] | 0 |
find-the-number-of-good-pairs-i | Most Easy Python Solution[Beats 100%] | most-easy-python-solutionbeats-100-by-gn-0mtp | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | gnishant9761 | NORMAL | 2025-02-17T19:04:55.367737+00:00 | 2025-02-17T19:04:55.367737+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
find-the-number-of-good-pairs-i | base solution js | base-solution-js-by-kovalvladik-bszp | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | kovalvladik | NORMAL | 2025-02-17T16:15:12.802981+00:00 | 2025-02-17T16:15:12.802981+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['JavaScript'] | 0 |
find-the-number-of-good-pairs-i | Runtime 3 ms Beats 89.11% | runtime-3-ms-beats-8911-by-ajithajk46-bpov | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | ajithajk46 | NORMAL | 2025-02-17T06:36:31.202199+00:00 | 2025-02-17T06:36:31.202199+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
find-the-number-of-good-pairs-i | easy solution | easy-solution-by-haneen_ep-4jem | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | haneen_ep | NORMAL | 2025-02-17T05:23:53.646565+00:00 | 2025-02-17T05:23:53.646565+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['JavaScript'] | 0 |
find-the-number-of-good-pairs-i | EASY PYTHON CODE | easy-python-code-by-vishnuande2006-kjy7 | Code | vishnuande2006 | NORMAL | 2025-02-16T09:46:42.846540+00:00 | 2025-02-16T09:46:42.846540+00:00 | 2 | false |
# Code
```python3 []
class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int:
c = 0
for i in range(len(nums1)):
for j in range(len(nums2)):
if nums1[i]%(nums2[j]*k) == 0:
c += 1
return c
``` | 0 | 0 | ['Python3'] | 0 |
find-the-number-of-good-pairs-i | Java&JS&TS Solution (JW) | javajsts-solution-jw-by-specter01wj-rlyj | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | specter01wj | NORMAL | 2025-02-12T23:26:59.851516+00:00 | 2025-02-12T23:26:59.851516+00:00 | 10 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Array', 'Hash Table', 'Java', 'TypeScript', 'JavaScript'] | 0 |
find-the-number-of-good-pairs-i | Simple brute force implementation (BEATS 100%) | simple-brute-force-implementation-beats-nk32x | Complexity
Time complexity:
Space complexity:
Code | Aashif_AK | NORMAL | 2025-02-12T11:18:43.413920+00:00 | 2025-02-12T11:18:43.413920+00:00 | 0 | false |
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int x = 0;
for (int i=0; i<nums1.length;i... | 0 | 0 | ['Java'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | [C++]--Just using recursion, very Clean and Easy to understand--O(n^2) | c-just-using-recursion-very-clean-and-ea-j89c | So, we can know that for a fixed root, the left subtree elements and the right subtree elements are also fixed.\n\nWe can find the left subtree elements which a | aincrad-lyu | NORMAL | 2020-08-30T04:13:32.314186+00:00 | 2020-08-30T08:51:26.622799+00:00 | 20,336 | false | So, we can know that for a fixed root, the left subtree elements and the right subtree elements are also fixed.\n\nWe can find the ``left subtree elements`` which are all the elements that is **smaller** than root value, and ``right subtree elements`` which are **greater** than root value.\n\nAnd in order to make it id... | 167 | 1 | [] | 19 |
number-of-ways-to-reorder-array-to-get-same-bst | Python in 6 short lines with easy explanation | python-in-6-short-lines-with-easy-explan-i3yw | We separate all the elements into two lists, depending on whether they are less than or more than the root. Then we recurse on those left and right sublists. | daciuk | NORMAL | 2020-08-30T04:06:35.358544+00:00 | 2020-08-30T04:11:28.620463+00:00 | 9,670 | false | We separate all the elements into two lists, depending on whether they are less than or more than the root. Then we recurse on those left and right sublists. The combination is for the macro ordering between left and right, and the recursive factors are for the internal ordering of left and right themselves. I minus... | 119 | 3 | [] | 10 |
number-of-ways-to-reorder-array-to-get-same-bst | Python 🐍 Easy & Fast Solution | python-easy-fast-solution-by-souvik_bane-xr81 | Intuition\n Describe your first thoughts on how to solve this problem. \n\nHere are my initial thoughts on how to solve this problem:\n\n- The base case of the | Souvik_Banerjee-2020 | NORMAL | 2023-06-16T02:15:50.003331+00:00 | 2023-06-16T02:15:50.003349+00:00 | 7,859 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nHere are my initial thoughts on how to solve this problem:\n\n- The base case of the recursive function f is when the length of the input list nums is less than or equal to 2, in which case there is only one way to arrange the numbers. ... | 65 | 0 | ['Array', 'Math', 'Divide and Conquer', 'Dynamic Programming', 'Python3'] | 10 |
number-of-ways-to-reorder-array-to-get-same-bst | [Java] Clean code uses Yang Hui's/Pascal's Triangle With Explanation | java-clean-code-uses-yang-huispascals-tr-ovoz | This is actually a mathematical problem that can be solved by combination calculation, what\'d you do is basically arranging left and right sub-trees in correct | chyyyy | NORMAL | 2020-08-30T07:34:26.532546+00:00 | 2020-08-30T23:34:56.325906+00:00 | 7,118 | false | This is actually a mathematical problem that can be solved by combination calculation, what\'d you do is basically arranging left and right sub-trees in correct order but in all possible combinations.\nFor example for array ```[3,6,4,1]```\n```\n[3,6,4,1] left sub tree is [1], right tree is [6,4], \nwe just need to kee... | 56 | 0 | [] | 6 |
number-of-ways-to-reorder-array-to-get-same-bst | C++/Python. Question explained. Then detailed solution. Short. Fast. Readable. | cpython-question-explained-then-detailed-659a | Question can seem confusing. Note that we are inserting the numbers in the binary search tree in exactly the same order as they occur in the input array. \n\nTh | axat-priy | NORMAL | 2020-08-30T08:57:57.183923+00:00 | 2020-08-31T01:50:08.760819+00:00 | 7,122 | false | Question can seem confusing. Note that we are inserting the numbers in the binary search tree in *exactly the same order as they occur in the input array*. \n\nThis is a good time to recall a fact which may seem very obvious but is crucial to understand the question: *For a fixed sequence of insertion, the number of bi... | 55 | 2 | ['C', 'Python'] | 4 |
number-of-ways-to-reorder-array-to-get-same-bst | [ BST ]✅ || Hard to Easy🔥 || C++⭐, Java🌟 & Python Clear ❄️ | bst-hard-to-easy-c-java-python-clear-by-pqq9s | Intuition\n- To construct a BST, we need to select a root node and divide the remaining elements into two groups: the left subtree (containing elements smaller | iamsanko | NORMAL | 2023-06-16T03:24:01.608432+00:00 | 2023-06-20T02:14:02.415909+00:00 | 8,080 | false | # Intuition\n- To construct a BST, we need to select a root node and divide the remaining elements into two groups: the left subtree (containing elements smaller than the root) and the right subtree (containing elements larger than the root). The order of elements within each subtree doesn\'t matter as long as the rela... | 30 | 0 | ['Divide and Conquer', 'Dynamic Programming', 'C++'] | 3 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ DFS + Comb | c-dfs-comb-by-votrubac-9bct | cpp\nint dp[1001][1001] = {};\nint comb(int n, int m) {\n return n == 0 || m == 0 ? 1 :\n dp[n][m] ? dp[n][m] : \n dp[n][m] = (comb(n - 1, | votrubac | NORMAL | 2020-09-01T00:44:37.037364+00:00 | 2020-09-01T00:45:26.669317+00:00 | 4,886 | false | ```cpp\nint dp[1001][1001] = {};\nint comb(int n, int m) {\n return n == 0 || m == 0 ? 1 :\n dp[n][m] ? dp[n][m] : \n dp[n][m] = (comb(n - 1, m) + comb(n, m - 1)) % 1000000007;\n}\nlong dfs(vector<int>& n) {\n if (n.size() <= 1)\n return 1;\n vector<int> n1, n2;\n copy_if(begin(n), ... | 27 | 8 | [] | 5 |
number-of-ways-to-reorder-array-to-get-same-bst | [Python] O(n^2) post-order traversal | python-on2-post-order-traversal-by-yanru-ip0h | Idea\nThe key idea is to maintain the topological order of the BST. \nMore specifically, this solution works in this way. \n\nStep 1\nFor every node, given that | yanrucheng | NORMAL | 2020-08-30T04:01:13.907314+00:00 | 2020-08-30T04:12:01.611862+00:00 | 2,942 | false | ## Idea\nThe key idea is to maintain the topological order of the BST. \nMore specifically, this solution works in this way. \n\n**Step 1**\nFor every node, given that its left subtree has `L` children and its right subtree has `R` children. We denote the number of way to schedule the placement of `L + R` nodes as `N(L... | 27 | 3 | [] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | O(N) simple&fast | on-simplefast-by-617280219-tchx | First, the answer can be expressed as n!/Product(size(i),i=1..n)-1, where size(i) denotes the size of subtree with root i.\nThen we have some observations:\n1. | 617280219 | NORMAL | 2020-09-10T08:25:12.704430+00:00 | 2020-09-10T08:26:26.169909+00:00 | 3,105 | false | First, the answer can be expressed as n!/Product(size(i),i=1..n)-1, where size(i) denotes the size of subtree with root i.\nThen we have some observations:\n1. any node in subtree must be inserted after the root\n2. any subtree contains a consecutive interval\n3. after any insertion step, any remaining interval will fi... | 23 | 0 | [] | 5 |
number-of-ways-to-reorder-array-to-get-same-bst | Java Solution for Number of Ways to Reorder Array to Get Same BST Problem | java-solution-for-number-of-ways-to-reor-hfb4 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks for the number of different ways to reorder the given array nums such | Aman_Raj_Sinha | NORMAL | 2023-06-16T03:24:12.256675+00:00 | 2023-06-16T03:24:12.256692+00:00 | 7,226 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks for the number of different ways to reorder the given array nums such that the constructed Binary Search Tree (BST) is identical to the original BST formed from nums. We can solve this problem recursively by considering t... | 20 | 0 | ['Java'] | 4 |
number-of-ways-to-reorder-array-to-get-same-bst | 2 Easiest C++ Solution🔥 || DP🔥 || Fastest 🔥🔥🔥 | 2-easiest-c-solution-dp-fastest-by-black-xr3q | \n# Approach : Dynamic Programming\n Describe your approach to solving the problem. \n\n# Code 1\n\nclass Solution {\n vector<vector<long long int>> dp;\n | Black_Mamba01 | NORMAL | 2023-06-16T01:25:00.542460+00:00 | 2023-06-16T18:15:22.010699+00:00 | 16,481 | false | \n# Approach : Dynamic Programming\n<!-- Describe your approach to solving the problem. -->\n\n# Code 1\n```\nclass Solution {\n vector<vector<long long int>> dp;\n long long MOD = 1e9 + 7;\n\n unsigned long long solve(vector<int> &nums) {\n if (nums.size() <= 1) return 1;\n vector<int> l, r;\n ... | 19 | 0 | ['Divide and Conquer', 'Dynamic Programming', 'Combinatorics', 'C++'] | 4 |
number-of-ways-to-reorder-array-to-get-same-bst | 🏆C++ || Recursion || (IMP nCr) | c-recursion-imp-ncr-by-chiikuu-ty7c | Code\n\nclass Solution {\npublic:\n vector<vector<long long>>ncr;\n long long mod=1e9+7;\n long long ways(vector<int>&v,int n){\n if(n<=2)return | CHIIKUU | NORMAL | 2023-06-16T05:46:08.282451+00:00 | 2023-06-16T05:46:08.282483+00:00 | 4,351 | false | # Code\n```\nclass Solution {\npublic:\n vector<vector<long long>>ncr;\n long long mod=1e9+7;\n long long ways(vector<int>&v,int n){\n if(n<=2)return 1;\n vector<int>left,right;\n for(int i=1;i<n;i++){\n if(v[0]>v[i])left.push_back(v[i]);\n else right.push_back(v[i]);... | 17 | 0 | ['Math', 'Tree', 'Recursion', 'Combinatorics', 'C++'] | 3 |
number-of-ways-to-reorder-array-to-get-same-bst | Didn't see any Java solution here, because nobody knows how to mod? | didnt-see-any-java-solution-here-because-okfn | Sadly me neither\n```\nclass Solution {\n long mod = (int) 1e9 + 7;\n public int numOfWays(int[] nums) {\n List list = new LinkedList<>();\n | JulesX01 | NORMAL | 2020-08-30T04:24:44.000623+00:00 | 2020-08-30T04:24:44.000655+00:00 | 2,290 | false | Sadly me neither\n```\nclass Solution {\n long mod = (int) 1e9 + 7;\n public int numOfWays(int[] nums) {\n List<Integer> list = new LinkedList<>();\n for(int i:nums){\n list.add(i);\n }\n\n return (int)(((helper(list)-1)%mod) % mod);\n }\n \n private long helper(Lis... | 17 | 1 | [] | 8 |
number-of-ways-to-reorder-array-to-get-same-bst | JavaScript | With Comments and Explanation [100%, 100%] (Combination + Mathematics) | javascript-with-comments-and-explanation-fcgp | Approach\nFind the number of ways to sort a list by replacing two elements at a time in given array\n\nPlease upvote me if you like this solution\n\n\n# Code\n\ | JayPokale | NORMAL | 2023-06-16T03:38:00.436970+00:00 | 2023-06-16T04:26:05.209771+00:00 | 1,357 | false | # Approach\nFind the number of ways to sort a list by replacing two elements at a time in given array\n```\nPlease upvote me if you like this solution\n```\n\n# Code\n```\n// Main Function\nvar numOfWays = function(nums) {\n return (helper(nums) - 1n) % BigInt(1e9+7) // Return modulo value\n};\n\n// Function return ... | 16 | 1 | ['JavaScript'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Simple (divide and conquer) Python solution with explanation | simple-divide-and-conquer-python-solutio-15d3 | Consider example 2 in the problem:Observations
The first number cannot be rearranged because moving it will change the root of the tree.
Swapping any numbers be | cthlo | NORMAL | 2021-06-08T07:31:28.392859+00:00 | 2025-01-20T23:11:45.109589+00:00 | 1,668 | false | Consider example 2 in the problem:

```
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]
```
### O... | 14 | 0 | ['Divide and Conquer', 'Python'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Video Solution | Java C++ Python | video-solution-java-c-python-by-jeevanku-8vm8 | \n\n\n\n\n\n\n\n\nclass Solution {\n long mod = (long)1e9 + 7;\n long[][] table;\n public int numOfWays(int[] nums) {\n int n = nums.length;\n | jeevankumar159 | NORMAL | 2023-06-16T04:49:09.497491+00:00 | 2023-06-16T04:49:41.612248+00:00 | 2,004 | false | \n\n\n\n<iframe width="560" height="315" src="https://www.youtube.com/embed/N8MDBYhF4dM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>\n\n\n\n```\nclass Solution {\n long mod = (long)1... | 12 | 0 | ['C', 'Python', 'Java'] | 3 |
number-of-ways-to-reorder-array-to-get-same-bst | Java simple DFS | java-simple-dfs-by-hobiter-hig3 | 1, once root is fixed, find all following nums < root, list left, to form left subtree;\nfind all following nums >root, list right, to form left subtree;\n2, fi | hobiter | NORMAL | 2020-09-04T02:45:24.784580+00:00 | 2020-09-04T02:55:14.873038+00:00 | 1,527 | false | 1, once root is fixed, find all following nums < root, list<Integer> left, to form left subtree;\nfind all following nums >root, list<Integer> right, to form left subtree;\n2, find all combinations, comb, to select left.size() positions for left from all following positions after root, left.size() + right.size().\n3, r... | 12 | 1 | [] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | [Java] Accepted Java solution | java-accepted-java-solution-by-mcjiang-czf2 | Didn\'t really enjoy the puzzle, started with long data type and kept MOD\'ing whenever I can but still overflow. I thought it was my algorithm but switching to | mcjiang | NORMAL | 2020-08-30T04:39:29.047764+00:00 | 2020-08-30T04:50:06.795897+00:00 | 2,121 | false | Didn\'t really enjoy the puzzle, started with long data type and kept MOD\'ing whenever I can but still overflow. I thought it was my algorithm but switching to BigInteger got it accepted.\n\nWell.. I guess it\'s simply not so java-friendly?\n\n**If LeetCode can hear me, I would really suggest we limit test cases below... | 12 | 0 | [] | 4 |
number-of-ways-to-reorder-array-to-get-same-bst | detailed explanation - Java simple recursive divide and conquer solution | detailed-explanation-java-simple-recursi-2l2n | PLEASE UPVOTE IF THE SOLUTION WAS USEFUL !!\n\n\nThere are different steps in order to come up with this solution:\n1. logic\n2. maths formula(permutation and | niranjvin | NORMAL | 2021-09-16T16:55:35.808679+00:00 | 2021-09-17T16:13:03.670989+00:00 | 2,677 | false | ***PLEASE UPVOTE IF THE SOLUTION WAS USEFUL !!***\n\n\nThere are different steps in order to come up with this solution:\n1. logic\n2. maths formula(permutation and combination)\n3. algorithm - divide and conquer\n4. optimisation(space) - modular inverse\n5. optimisation(time) - moduler exponentiation\n\nI will ex... | 10 | 0 | ['Divide and Conquer', 'Java'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Python 3 || 9 lines, recursion || T/S: 99% / 87% | python-3-9-lines-recursion-ts-99-87-by-s-0j1x | \nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n\n def dp(nums: List[int]) -> int:\n\n n = len(nums)\n if n < | Spaulding_ | NORMAL | 2023-06-16T07:38:58.543443+00:00 | 2024-06-06T06:20:25.584021+00:00 | 1,286 | false | ```\nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n\n def dp(nums: List[int]) -> int:\n\n n = len(nums)\n if n < 3: return 1\n root, left, right = nums[0], [], []\n\n for x in nums:\n if x < root: left .append(x)\n e... | 9 | 0 | ['Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Combination formula : C(m,n) = C(m-1,n) + C(m-1,n-1). Pre compute | combination-formula-cmn-cm-1n-cm-1n-1-pr-xgtb | C(m,n) = C(m-1,n) + C(m-1,n-1)\nC(m,n) represents the formula for combinations.\n\n\npublic class Solution\n{\n long mod = (long)Math.Pow(10, 9) + 7;\n pu | leoooooo | NORMAL | 2020-08-30T09:43:30.257151+00:00 | 2020-08-30T09:50:17.591332+00:00 | 1,301 | false | C(m,n) = C(m-1,n) + C(m-1,n-1)\nC(m,n) represents the formula for combinations.\n\n```\npublic class Solution\n{\n long mod = (long)Math.Pow(10, 9) + 7;\n public int NumOfWays(int[] nums)\n {\n int n = nums.Length;\n long[,] dp = new long[n, n];\n for (int i = 0; i < n; i++)\n {\n ... | 9 | 1 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ | Dynamic Programming | | c-dynamic-programming-by-ashish_madhup-fgwz | Intuition\n Describe your first thoughts on how to solve this problem. Upon reviewing the provided code, it appears to be a solution to a problem that involves | ashish_madhup | NORMAL | 2023-06-16T06:31:27.994920+00:00 | 2023-06-16T06:32:15.124201+00:00 | 2,538 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->Upon reviewing the provided code, it appears to be a solution to a problem that involves counting the number of ways to arrange a given sequence of numbers. The solution seems to be using a combination formula and a recursive approach to ca... | 8 | 0 | ['Array', 'Dynamic Programming', 'Recursion', 'Counting', 'C++'] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | Number of Ways to Reorder Array to Get Same BST | C++ | Recursive | number-of-ways-to-reorder-array-to-get-s-vjhn | The problem could be solved using recusion as the problem could be broken down into smaller overlapping subproblems.\n\nThe first node would always be the root | tushargarg | NORMAL | 2020-08-30T04:06:53.077238+00:00 | 2020-08-30T04:20:29.153074+00:00 | 887 | false | The problem could be solved using recusion as the problem could be broken down into smaller overlapping subproblems.\n\nThe first node would always be the root node so it can\'t be rearranged to any other place. For the rest of the nodes the nodes smaller than the root node would have the same ordering and the nodes la... | 7 | 2 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Approach Explained With Observation || Easy To Understand | approach-explained-with-observation-easy-rbes | Intuition\n\nDifferent permutation that yield same bst as given \n OBSERVATION :- \n [1] ROOT IS SAME \n [2] NOW, COMES LEFT AND RIGHT PART | shubh08am | NORMAL | 2023-06-16T15:38:39.486226+00:00 | 2023-06-16T15:38:39.486252+00:00 | 1,163 | false | # Intuition\n```\nDifferent permutation that yield same bst as given \n OBSERVATION :- \n [1] ROOT IS SAME \n [2] NOW, COMES LEFT AND RIGHT PART \n [3] RELATIVE POSITION IS FIXED OF LEFT AND RIGHT PART\n [4] COUNT NO OF PERMUTATION \n [5] LEFT N-1 PLACE TO BE FILLED \n [... | 5 | 0 | ['Math', 'Dynamic Programming', 'Recursion', 'Combinatorics', 'C++'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Simple Python Solution O(n^2) | simple-python-solution-on2-by-niraj243-vn9m | Code\n\nclass Solution(object):\n def numOfWays(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n self.ans | niraj243 | NORMAL | 2023-06-16T10:21:00.276888+00:00 | 2023-06-16T15:26:37.172771+00:00 | 464 | false | # Code\n```\nclass Solution(object):\n def numOfWays(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n self.ans = 1\n mod = 1000000007\n class BST:\n self.right = None\n self.left = None\n self.val = None\n se... | 5 | 0 | ['Binary Search Tree', 'Combinatorics', 'Python'] | 7 |
number-of-ways-to-reorder-array-to-get-same-bst | Best Solution 😎💪🏼 | best-solution-by-kchheda97-c48f | Approach\nThe approach used in the code involves a depth-first search (DFS) algorithm. The dfs function takes three parameters: i represents the current index, | kchheda97 | NORMAL | 2023-06-16T00:23:14.432201+00:00 | 2023-06-16T01:12:22.338322+00:00 | 236 | false | # Approach\nThe approach used in the code involves a depth-first search (DFS) algorithm. The dfs function takes three parameters: i represents the current index, l represents the lower bound, and h represents the upper bound.\n\nThe function checks if the upper bound h is one greater than the lower bound l. If so, it m... | 5 | 0 | ['Python3'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ explanation for 1569 | c-explanation-for-1569-by-tktripathy-qyan | How does this work? Explaining it so that it might help some folks.\n\n\nCase I: Consider a BST: [4,2,1,3,5] \n 4 [ level 0 ]\n 2 | tktripathy | NORMAL | 2020-09-06T01:14:16.962024+00:00 | 2020-09-06T06:42:58.247211+00:00 | 733 | false | How does this work? Explaining it so that it might help some folks.\n\n```\nCase I: Consider a BST: [4,2,1,3,5] \n 4 [ level 0 ]\n 2 5 [ level 1 ]\n 1 3 \nWe get these valid ways at level 0:\n [4,2,1,3,5] \n [4,2,1,5,3]\n [4,2,5,1,3]\n [4,5,2,1,3]\n \n [4,... | 5 | 0 | [] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ || WITH FULL EXPLANATION || EASY | c-with-full-explanation-easy-by-neha0312-gxm7 | We know that in bst all the nodes to the left are less than the root node and all the nodes to the right are greater than the root node. So in order to create s | neha0312 | NORMAL | 2023-06-16T05:25:03.407422+00:00 | 2023-06-16T05:25:03.407448+00:00 | 424 | false | We know that in bst all the nodes to the left are less than the root node and all the nodes to the right are greater than the root node. So in order to create same bst we will have to use same root as first node and consider relative ordering independently of left and right sub part.\n\nIt took me while to understand t... | 4 | 0 | ['Binary Search Tree', 'C', 'Combinatorics'] | 3 |
number-of-ways-to-reorder-array-to-get-same-bst | w Explanation||C++/Python using Math Pascal's triangle/comb Beats 96.74% | w-explanationcpython-using-math-pascals-yweky | Intuition\n Describe your first thoughts on how to solve this problem. \nThe first element in array must be the root. Then divide the array into left subtree an | anwendeng | NORMAL | 2023-06-16T05:18:10.415690+00:00 | 2023-06-16T18:29:34.470713+00:00 | 1,442 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first element in array must be the root. Then divide the array into left subtree and right subtree! \n\nUse recursion, if the subproblems for left subtree and right subtree are solved, with the returning number l and r, Use the follow... | 4 | 0 | ['Math', 'Divide and Conquer', 'Dynamic Programming', 'C++', 'Python3'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ || Explained | c-explained-by-punityadav_2003-t686 | \n\n# Approach\n The solution first calculates the binomial coefficients using dynamic programming and stores them in a 2D vector comb. \nThen, it defines a rec | PunitYadav_2003 | NORMAL | 2023-06-16T02:37:49.478963+00:00 | 2023-06-16T02:37:49.478989+00:00 | 2,976 | false | \n\n# Approach\n The solution first calculates the binomial coefficients using dynamic programming and stores them in a 2D vector comb. \nThen, it defines a recursive function dfs that takes as input a subarray of nums and returns the number of ways to reorder that subarray such that the BST formed is identical to the ... | 4 | 0 | ['Dynamic Programming', 'Combinatorics', 'C++'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | ⭐DP Recursion using Python 🔥Layman's Code 🔥⭐ | dp-recursion-using-python-laymans-code-b-88xa | An upvote would be appreciating...\n--\n# Intuition\nThe problem asks us to find the number of ways to reorder an array in order to obtain the same binary searc | ShreejitCheela | NORMAL | 2023-06-16T01:57:11.736848+00:00 | 2023-06-16T01:59:40.435007+00:00 | 1,013 | false | **An upvote would be appreciating...**\n--\n# Intuition\nThe problem asks us to find the number of ways to reorder an array in order to obtain the same binary search tree (BST) structure. We can approach this problem by counting the number of valid BSTs that can be formed from the given array.\n\n# Approach\nTo solve t... | 4 | 0 | ['Dynamic Programming', 'Binary Search Tree', 'Recursion', 'Binary Tree', 'Python3'] | 3 |
number-of-ways-to-reorder-array-to-get-same-bst | Ruby: array partitioning and combinatorics | ruby-array-partitioning-and-combinatoric-dowu | Code\nruby\nMOD = 1_000_000_007\n\ndef num_of_ways(nums) = bst_permutations(nums) - 1\n\ndef bst_permutations(nums)\n if nums.empty?\n 1\n else\n root_v | lacrosse | NORMAL | 2023-03-27T20:56:07.326998+00:00 | 2023-03-27T20:56:07.327024+00:00 | 97 | false | # Code\n```ruby\nMOD = 1_000_000_007\n\ndef num_of_ways(nums) = bst_permutations(nums) - 1\n\ndef bst_permutations(nums)\n if nums.empty?\n 1\n else\n root_val = nums.shift\n partitions = nums.partition { _1 < root_val }\n comb(*partitions.map(&:size)) * partitions.map { bst_permutations _1 }.reduce(:*) %... | 4 | 0 | ['Array', 'Math', 'Combinatorics', 'Ruby'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | O(NlogN) asymptotically OPTIMAL solution. | onlogn-asymptotically-optimal-solution-b-t8oh | O(N^2) isn\'t optimal\n\nBefore reading this, you should understand the recursive solution by reading other posts (C++, Java, doing division is a bit complicate | cai_lw | NORMAL | 2020-08-30T21:15:53.274127+00:00 | 2020-08-31T00:17:16.300446+00:00 | 575 | false | **O(N^2) isn\'t optimal**\n\nBefore reading this, you should understand the recursive solution by reading other posts ([C++](https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/discuss/819369/C%2B%2B-Just-using-recursion-very-Clean-and-Easy-to-understand-O(n2)), [Java](https://leetcode.com/pro... | 4 | 0 | [] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | [Python3] math-ish | python3-math-ish-by-ye15-q7ci | \n\nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n \n def fn(nums): \n """Post-order traversal."""\n if | ye15 | NORMAL | 2020-08-30T04:09:36.695102+00:00 | 2020-08-30T04:09:36.695133+00:00 | 757 | false | \n```\nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n \n def fn(nums): \n """Post-order traversal."""\n if len(nums) <= 1: return len(nums) # boundary condition \n ll = [x for x in nums if x < nums[0]]\n rr = [x for x in nums if x > nums[0]]... | 4 | 1 | ['Python3'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Java || 7ms || DFS with Pascal's Triangle | java-7ms-dfs-with-pascals-triangle-by-du-6a0v | \nclass Solution {\n // Pascal\'s Triangle is built only once per SUBMIT, and then can be \n // used for all other test cases of the SUBMIT.\n private | dudeandcat | NORMAL | 2023-06-16T14:41:56.716292+00:00 | 2023-08-26T19:04:29.920167+00:00 | 280 | false | ```\nclass Solution {\n // Pascal\'s Triangle is built only once per SUBMIT, and then can be \n // used for all other test cases of the SUBMIT.\n private static int[][] pascalsTriangle = createPascalsTriangle(1001);\n private static final int MOD = 1_000_000_007;\n\n \n // Main method called by leetco... | 3 | 0 | ['Java'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Rust | “i dont like trees” faster-then-editorial approach | rust-i-dont-like-trees-faster-then-edito-z9gf | Intuition\nThe answer will be the product for all nodes of $\binom{k+m}{m}$ where $k$ and $m$ are sizes of left and right subtree of the node, respectively.\n\n | tifv | NORMAL | 2023-06-16T04:56:08.415159+00:00 | 2023-06-16T04:57:45.635517+00:00 | 251 | false | # Intuition\nThe answer will be the product for all nodes of $\\binom{k+m}{m}$ where $k$ and $m$ are sizes of left and right subtree of the node, respectively.\n\nWe want to compute the sizes of subtrees somehow. The easiest way would be to either build the tree directly or somehow iterate through it via function recur... | 3 | 0 | ['Rust'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | C# Solution because there weren't any others | c-solution-because-there-werent-any-othe-81ay | Approach\nGenerates a table of all binomial coefficients to reference, then calculates the number of combinations for all subtrees with recursion. \nSame as the | GOBurrito | NORMAL | 2023-06-16T03:52:44.792322+00:00 | 2023-06-16T03:54:46.663142+00:00 | 801 | false | # Approach\nGenerates a table of all binomial coefficients to reference, then calculates the number of combinations for all subtrees with recursion. \nSame as the editorial suggests.\n\nI first tried a solution calculating and caching factorials, but it was not fun trying to work with values as large as 35!\nThis one i... | 3 | 0 | ['C#'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Python3 solution Fast With ExPlanation | python3-solution-fast-with-explanation-b-a9ta | Intuition\n Describe your first thoughts on how to solve this problem. \nWe can approach the problem by using a recursive approach. \n# Approach\n Describe your | Obose | NORMAL | 2023-01-30T23:01:55.039455+00:00 | 2023-01-30T23:01:55.039487+00:00 | 977 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can approach the problem by using a recursive approach. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo solve this problem, we can use a recursive approach. We start from the first element of the array, and fi... | 3 | 0 | ['Array', 'Math', 'Divide and Conquer', 'Dynamic Programming', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Detailed Explanation | (Hopefully) Easy to understand | Python Solution | detailed-explanation-hopefully-easy-to-u-n5sl | Intuition and Approach\n Describe your first thoughts on how to solve this problem. \nLet\'s start small\n\nLook at this Binary Search Tree\n\n 2\n / \\\n 1 | with_love_kd | NORMAL | 2023-01-28T13:25:56.204712+00:00 | 2023-01-28T13:25:56.204846+00:00 | 183 | false | # Intuition and Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nLet\'s start small\n\nLook at this Binary Search Tree\n```\n 2\n / \\\n 1 3\n```\nWhat do you think is the valid array to form this BST?\n`[2,1,3]` and `[2,3,1]` both are correct here.\n\nLet\'s look at another BST\n```\... | 3 | 0 | ['Binary Search Tree', 'Combinatorics', 'Binary Tree', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ | Math | Combination Theory | Dynamic Programming | c-math-combination-theory-dynamic-progra-sow8 | 1569. Number of Ways to Reorder Array to Get Same BST\n\nIt\'s a interesting math problem. \n\nWe will solve this problem by recursion, it\'s a little similar t | sinkinben | NORMAL | 2021-12-27T13:59:40.147238+00:00 | 2021-12-28T03:26:43.602405+00:00 | 576 | false | **1569. Number of Ways to Reorder Array to Get Same BST**\n\nIt\'s a interesting math problem. \n\nWe will solve this problem by recursion, it\'s a little similar to dynamic programming.\n\n+ On the top level, there is no doubt that `nums[0]` is the root of BST.\n+ Remind that in BST, we have `left < root < right`.\n+ ... | 3 | 0 | ['Combinatorics'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Python3 original counting and if we want to print all bst list | python3-original-counting-and-if-we-want-mb3v | \nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n \n # Time O(N^2)\n # Space O(N) for recursion stack\n \n | walkon302 | NORMAL | 2021-08-27T18:21:40.769039+00:00 | 2021-08-27T18:21:40.769090+00:00 | 1,362 | false | ```\nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n \n # Time O(N^2)\n # Space O(N) for recursion stack\n \n def combine(n, k):\n \n def fac(n):\n s = 1\n for i in range(1, n + 1):\n s *= i\n ... | 3 | 0 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | No pascal's triangle or yang or combinatoral formula. Simple dp accepted and faster than 60% | no-pascals-triangle-or-yang-or-combinato-5g2m | 1) For the given list, 1st number is the root. Make a list of smaller and bigger numbers after this. And do the following recursively for each of them.\n2) We n | vivek_bansal | NORMAL | 2020-09-07T10:57:13.974626+00:00 | 2020-09-07T10:57:13.974678+00:00 | 304 | false | 1) For the given list, 1st number is the root. Make a list of smaller and bigger numbers after this. And do the following recursively for each of them.\n2) We need to merge bigger and smaller list together in a way that relative ordering of the number is maintained.\n```\n\'\'\'\nAssume dp[i][j] as the number of ways t... | 3 | 2 | [] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | Python short DP | python-short-dp-by-hjscoder-w91q | Explanation: \nI used recursive + dp to solve this problem. \nIf there is regulation by not using any library to generate Permutation Count, DP can come to he | hjscoder | NORMAL | 2020-08-30T23:15:32.568704+00:00 | 2020-08-30T23:25:41.944105+00:00 | 472 | false | <h4> Explanation: </h4>\nI used recursive + dp to solve this problem. \nIf there is regulation by not using any library to generate Permutation Count, DP can come to help to get the Permutation Count. Here is the code:\n\n\n```\nclass Solution:\n def numOfWays(self, nums: List[int]) -> int:\n dp = [[1 for r i... | 3 | 0 | [] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | [Python3] Divide and Conquer | detailed explanation | python3-divide-and-conquer-detailed-expl-6pxf | The idea is divide and conquer:\n1) To get the same BST, we must first insert the root(the first element in the array), then we can insert its left subtree and | xuanyuchen0 | NORMAL | 2020-08-30T14:44:09.031966+00:00 | 2020-09-01T05:15:38.414921+00:00 | 298 | false | The idea is divide and conquer:\n1) To get the same BST, we must first insert the root(the first element in the array), then we can insert its left subtree and right subtree.\n\n2) For both subtrees, we need to first insert root as well;\n\n3) when we have numbers of ways to arrange left subtree and right subtree, we ... | 3 | 0 | [] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Easiest Solution!!! | bests 78% of the java coders | | easiest-solution-bests-78-of-the-java-co-6nsc | Intuition\nThe intuition behind this code is to use a recursive approach to count the number of ways to split a list of numbers into two non-empty sublists, suc | maheshkumarofficial | NORMAL | 2023-11-11T01:07:01.151868+00:00 | 2023-11-11T01:07:01.151889+00:00 | 68 | false | # Intuition\nThe intuition behind this code is to use a recursive approach to count the number of ways to split a list of numbers into two non-empty sublists, such that the root of the tree is the largest number in the left sublist and the smallest number in the right sublist.\n\nWe can do this by first finding the roo... | 2 | 0 | ['Java'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | EXPLAINED | Beginer friendly | Recursion | C++ | 90% FAST | O(n^2) | explained-beginer-friendly-recursion-c-9-7jlx | Intuition\n Describe your first thoughts on how to solve this problem. \n\nIf you are not familiar with how to form a BST form the array, you should definitely | joshuamahadevan1 | NORMAL | 2023-06-17T05:02:51.564370+00:00 | 2023-06-17T05:22:10.572565+00:00 | 551 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nIf you are not familiar with how to form a BST form the array, you should definitely check it out online. [resource link](https://devcamp.com/trails/development-soft-skills/campsites/understanding-algorithms/guides/how-to-create-binary-... | 2 | 0 | ['C++'] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | [Python 3] Recursion, comb, beats 95+% | python-3-recursion-comb-beats-95-by-patr-riy2 | Intuition\nThe first idea I had here was that each root has to come before any of its children in the list, otherwise the tree will be out of order. This then m | patrickpeng168 | NORMAL | 2023-06-16T20:36:27.917333+00:00 | 2023-06-16T20:36:27.917364+00:00 | 58 | false | # Intuition\nThe first idea I had here was that each root has to come before any of its children in the list, otherwise the tree will be out of order. This then made me think of splitting the problem into subtrees (recursion). For each tree, the total number of combinations possible is the number of ways to shuffle its... | 2 | 0 | ['Recursion', 'Python3'] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | The World Needs Another Python Solution | the-world-needs-another-python-solution-99cwt | Approach\n\nWe use recursion. The steps below explain the solution.\n\n1. The root must be inserted first.\n2. The order of nodes inserted in the left subtree i | trentono | NORMAL | 2023-06-16T20:21:34.202318+00:00 | 2023-06-17T16:05:21.108315+00:00 | 65 | false | # Approach\n\nWe use recursion. The steps below explain the solution.\n\n1. The root must be inserted first.\n2. The order of nodes inserted in the left subtree is independent of the order of nodes inserted in the right subtree.\n3. The nodes in the left subtree can be inserted in any order relative to the nodes in the... | 2 | 0 | ['Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | [Java][Python] 2 Approch|| Faster than 96.8% Using Pascal's Tringle in java and combinatories , BST | javapython-2-approch-faster-than-968-usi-qc3d | Intuition\n Describe your first thoughts on how to solve this problem. \n- Intuition for Java:(using Pascal\'s Tringle)\nwe uses a recursive approach combined w | Anamika_aca | NORMAL | 2023-06-16T18:38:42.066667+00:00 | 2023-06-16T18:38:42.066685+00:00 | 189 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- **Intuition for Java:(using Pascal\'s Tringle)**\nwe uses a recursive approach combined with the Pascal\'s Triangle to calculate the number of ways to arrange the elements in the input array. It separates the elements into two sublists ... | 2 | 0 | ['Java', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Python short and clean. | python-short-and-clean-by-darshan-as-glhy | Approach\nTL;DR, Similar to Editorial solution.\n\n# Complexity\n- Time complexity: O(n ^ 2)\n\n- Space complexity: O(n ^ 2)\n\n# Code\npython\nclass Solution:\ | darshan-as | NORMAL | 2023-06-16T18:38:12.455493+00:00 | 2023-06-16T18:38:12.455514+00:00 | 314 | false | # Approach\nTL;DR, Similar to [Editorial solution](https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/editorial/).\n\n# Complexity\n- Time complexity: $$O(n ^ 2)$$\n\n- Space complexity: $$O(n ^ 2)$$\n\n# Code\n```python\nclass Solution:\n def numOfWays(self, nums: list[int]) -> int:\n ... | 2 | 0 | ['Divide and Conquer', 'Binary Search Tree', 'Combinatorics', 'Python', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | ✅100% Fast | C++ | Easy to understand & clean approach| TC: O(N) 🔥| BST & Factorial Precomputation | 100-fast-c-easy-to-understand-clean-appr-xam2 | Intuition\nPrecomputation of factorial of a number till 1000 is required.\nDeclare a global variable to keep the final answer(say ans).\n \nStep1: Create a bina | prabhash_iitp | NORMAL | 2023-06-16T18:00:16.947344+00:00 | 2023-07-05T12:25:14.162174+00:00 | 552 | false | # Intuition\nPrecomputation of factorial of a number till 1000 is required.\nDeclare a global variable to keep the final answer(say ans).\n \nStep1: Create a binary search tree.\nStep2: For every node, find the no of nodes in left subtree(say L) and no of nodes in right subtree(say R) then do **ans = ans* (L+R combina... | 2 | 0 | ['C++'] | 3 |
number-of-ways-to-reorder-array-to-get-same-bst | Finding topological sorts (clean code + explanation) | finding-topological-sorts-clean-code-exp-klag | Intuition\nThis problem can be rephrased as finding the number of ways to topological sort the BST. We are given one, which can be used to build a tree, and the | captainspongebob1 | NORMAL | 2023-06-16T11:57:38.534989+00:00 | 2023-06-16T12:03:21.184021+00:00 | 66 | false | # Intuition\nThis problem can be rephrased as finding the number of ways to topological sort the BST. We are given one, which can be used to build a tree, and the task is to find the rest.\n\n# Approach\nAfter building our tree, define a helper function `h(x)` that returns the number of topological sortings for the tre... | 2 | 0 | ['Math', 'Tree', 'Recursion', 'Combinatorics', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Easiest Explanation | Hard --> Easy | codestorywithMIK | easiest-explanation-hard-easy-codestoryw-725z | YouTube video link - Number of Ways to Reorder Array to Get Same BST\nMy Github Treasure - Number of Ways to Reorder Array to Get Same BST\n\n\n\nclass Solution | mazhar_mik | NORMAL | 2023-06-16T10:28:55.253127+00:00 | 2023-06-16T10:28:55.253146+00:00 | 60 | false | YouTube video link - [Number of Ways to Reorder Array to Get Same BST](https://www.youtube.com/watch?v=YMe9Q2yZvBo)\nMy Github Treasure - [Number of Ways to Reorder Array to Get Same BST](https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Mathematical/Number%20of%20Ways%20to%20Reorder%20Array%20to%20Get%20Same%... | 2 | 1 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Easy C++ solution (It's mainly about nCr) | easy-c-solution-its-mainly-about-ncr-by-9455f | I have calculated nCr using binary exponentiation and Class 12th nCr formula and Modular multiplicative inverse.\n\n```\nnCr= n! / r!(n-r)!\n\nclass Solution {\ | absolute-mess | NORMAL | 2023-06-16T10:08:10.318516+00:00 | 2023-06-16T10:08:10.318535+00:00 | 314 | false | I have calculated nCr using binary exponentiation and Class 12th nCr formula and Modular multiplicative inverse.\n\n```\nnCr= n! / r!*(n-r)!\n\nclass Solution {\npublic:\n long long fac[1001];\n int mod=1e9+7;\n void fact() { // To calculate the Factorial and mod for value in range\n fac[0]=1;\n ... | 2 | 1 | ['Divide and Conquer', 'C'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Simple Recursive Approach C++ code | simple-recursive-approach-c-code-by-meow-36pd | Intuition\nDivide and Conquer technique via recursion \n\n# Approach\nstoring left subtree in left array and right subtree in right array\ntotal number of ways | meowzies | NORMAL | 2023-06-16T09:40:52.885052+00:00 | 2023-06-16T09:41:18.139543+00:00 | 115 | false | # Intuition\nDivide and Conquer technique via recursion \n\n# Approach\nstoring left subtree in left array and right subtree in right array\ntotal number of ways can be achieved by no of arranging left subtree * no of ways of arranging right subtree * nCr\n\n\n# Code\n```\nclass Solution {\n int mod=1e9+7;\npublic:\... | 2 | 0 | ['Math', 'Divide and Conquer', 'Binary Search Tree', 'Recursion', 'C++'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | Java Solution | Permutations & Combinations | java-solution-permutations-combinations-8zcin | Code\n\nclass Solution {\n public int numOfWays(int[] nums) {\n List<Integer> arr=new ArrayList<>();\n for(int i=0; i<nums.length; i++) {\n | adarsh-mishra27 | NORMAL | 2023-06-16T07:38:14.401221+00:00 | 2023-06-16T07:40:20.920879+00:00 | 1,344 | false | # Code\n```\nclass Solution {\n public int numOfWays(int[] nums) {\n List<Integer> arr=new ArrayList<>();\n for(int i=0; i<nums.length; i++) {\n arr.add(nums[i]);\n }\n\n int n=nums.length;\n pascal=new long[n+1][n+1];\n pascal[0][0]=1;\n //nCr = n-1Cr-1 + ... | 2 | 0 | ['Divide and Conquer', 'Binary Search Tree', 'Combinatorics', 'Binary Tree', 'Java'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | [ C++ ] [ Dynamic Programming + DFS ] | c-dynamic-programming-dfs-by-sosuke23-d850 | Code\n\nclass Solution {\n int mod = 1e9 + 7;\n int combination(int k, int n, int mod) {\n if (k > n - k) k = n - k;\n vector<int> dp(k + 1) | Sosuke23 | NORMAL | 2023-06-16T01:04:46.647630+00:00 | 2023-06-16T01:04:46.647649+00:00 | 1,504 | false | # Code\n```\nclass Solution {\n int mod = 1e9 + 7;\n int combination(int k, int n, int mod) {\n if (k > n - k) k = n - k;\n vector<int> dp(k + 1);\n dp[0] = 1;\n for (int i = 1; i <= n; ++i) {\n for (int j = min(i, k); j > 0; --j) dp[j] = (dp[j] + dp[j - 1]) % mod;\n ... | 2 | 0 | ['C++'] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | Fastest Solution Yet | fastest-solution-yet-by-x7d309777-e0v2 | Intuition\nGiven 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 elem | AZ777 | NORMAL | 2023-06-16T00:06:33.112306+00:00 | 2023-06-19T18:28:43.069497+00:00 | 422 | false | # Intuition\nGiven 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 f... | 2 | 0 | ['Backtracking', 'Binary Search Tree', 'Recursion', 'Combinatorics', 'Python3'] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | [C++], O(n^2) But easy to understand | c-on2-but-easy-to-understand-by-amasub22-p333 | Intuition\nThe problem asks us to find the number of ways to reorder an input array nums such that a binary search tree (BST) constructed from the reordered arr | amasub222 | NORMAL | 2023-04-28T08:56:45.200650+00:00 | 2023-04-28T08:56:45.200682+00:00 | 462 | false | # Intuition\nThe problem asks us to find the number of ways to reorder an input array nums such that a binary search tree (BST) constructed from the reordered array is identical to the BST formed from the original array. This problem can be solved by first understanding the properties of a BST, and then using dynamic p... | 2 | 0 | ['Divide and Conquer', 'Dynamic Programming', 'Combinatorics', 'C++'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ simple and short DFS solution | c-simple-and-short-dfs-solution-by-cal_a-otca | \n# Code\n\nclass Solution {\n const int mod = 1e9 + 7;\n long inverse(long num) {\n if (num == 1) {\n return 1;\n }\n ret | cal_apple | NORMAL | 2023-01-22T08:49:57.593723+00:00 | 2023-01-22T08:49:57.593765+00:00 | 462 | false | \n# Code\n```\nclass Solution {\n const int mod = 1e9 + 7;\n long inverse(long num) {\n if (num == 1) {\n return 1;\n }\n return mod - mod / num * inverse(mod % num) % mod;\n }\n\n int dfs(vector<int>& nums) {\n int N = nums.size();\n if (N <= 2) {\n ... | 2 | 0 | ['C++'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | ✔ Python3 Solution | Combination | python3-solution-combination-by-satyam20-j9ku | Code\n\nclass Solution:\n def numOfWays(self, A):\n n = len(A)\n mod = 10 ** 9 + 7\n def dfs(i, l, h):\n if h == l + 1: retur | satyam2001 | NORMAL | 2022-12-22T07:25:13.477916+00:00 | 2022-12-22T07:25:13.477957+00:00 | 710 | false | # Code\n```\nclass Solution:\n def numOfWays(self, A):\n n = len(A)\n mod = 10 ** 9 + 7\n def dfs(i, l, h):\n if h == l + 1: return 1\n if l < A[i] < h:\n return (dfs(i + 1, l, A[i]) * dfs(i + 1, A[i], h) * math.comb(h - l - 2, A[i] - l - 1)) % mod\n ... | 2 | 0 | ['Recursion', 'Combinatorics', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Java | Recursive Solution - 88% fast | java-recursive-solution-88-fast-by-basel-aozy | \npublic class Solution {\n private Map<TreeNode, Integer> map;\n private int mod = 1000000007;\n \n public Solution() {\n map = new HashMap< | BaselAhmed | NORMAL | 2022-08-26T18:27:13.082306+00:00 | 2022-08-26T18:27:13.082358+00:00 | 1,350 | false | ``` \npublic class Solution {\n private Map<TreeNode, Integer> map;\n private int mod = 1000000007;\n \n public Solution() {\n map = new HashMap<>();\n }\n class TreeNode {\n public int val;\n public TreeNode left;\n public TreeNode right;\n\n public TreeNode(int va... | 2 | 0 | ['Dynamic Programming', 'Binary Search Tree', 'Combinatorics', 'Java'] | 1 |
number-of-ways-to-reorder-array-to-get-same-bst | JavaScript Solution | Recursion + Combination | javascript-solution-recursion-combinatio-uy2h | \nvar numOfWays = function(nums) {\n const x = numOfWaysHelper(nums) - 1n;\n return x % 1_000_000_007n;\n}\nvar numOfWaysHelper = function(nums) {\n if | mr-harry | NORMAL | 2021-12-28T13:47:36.405363+00:00 | 2021-12-28T13:47:36.405405+00:00 | 694 | false | ```\nvar numOfWays = function(nums) {\n const x = numOfWaysHelper(nums) - 1n;\n return x % 1_000_000_007n;\n}\nvar numOfWaysHelper = function(nums) {\n if(nums.length < 3)\n return 1n;\n \n const root = nums[0];\n const left = nums.filter(p => p < root);\n const right = nums.filter(p => p ... | 2 | 0 | ['JavaScript'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Python3 solution | python3-solution-by-_yash-0z8e | \nclass node:\n def __init__(self, val):\n self.val = val\n self.left = None\n self.right = None\n\nclass Solution:\n def BST(self, r | _yash_ | NORMAL | 2021-06-08T10:45:15.780881+00:00 | 2021-06-08T10:45:15.780916+00:00 | 1,601 | false | ```\nclass node:\n def __init__(self, val):\n self.val = val\n self.left = None\n self.right = None\n\nclass Solution:\n def BST(self, root, cur):\n if cur.val < root.val:\n if root.left == None:\n root.left = cur\n return\n else:\n ... | 2 | 0 | ['Python', 'Python3'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | [C++] algebra, module-inverse, comb(N, K) | c-algebra-module-inverse-combn-k-by-mult-0uw0 | \nclass Solution {\n const int64_t MOD = 1000000007;\n \n int64_t comb(int N, int K) {\n int64_t nx = 1;\n // i!\n for (int64_t i | multivacx | NORMAL | 2021-05-23T03:56:57.292242+00:00 | 2021-05-23T03:56:57.292270+00:00 | 550 | false | ```\nclass Solution {\n const int64_t MOD = 1000000007;\n \n int64_t comb(int N, int K) {\n int64_t nx = 1;\n // i!\n for (int64_t i = 2; i <= N; ++i) nx = nx * i % MOD;\n\n const int M = max(K, N - K) + 1;\n vector<int64_t> inv(M, 0), inv_kx(M, 0);\n inv[0] = inv_kx[0... | 2 | 0 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Easy and commented [C++] solution | easy-and-commented-c-solution-by-leetcod-4qps | \n#define ll long long\n\nclass Solution {\npublic:\n \n ll MOD=1e9L+7;\n ll dp[1001][1001]; // dp[i][j] = iCj (i choose j)\n \n ll util(vector<i | leetcode07 | NORMAL | 2020-11-14T18:33:21.116257+00:00 | 2020-11-14T18:33:21.116298+00:00 | 709 | false | ```\n#define ll long long\n\nclass Solution {\npublic:\n \n ll MOD=1e9L+7;\n ll dp[1001][1001]; // dp[i][j] = iCj (i choose j)\n \n ll util(vector<int>& v){\n int n=v.size();\n if(n==0) return 1; // base case when binary search tree is empty\n \n int root=v[0]; // root of the ... | 2 | 1 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Java | Bottom-Up DP + Recursion | O(n^2) Time Complexity | java-bottom-up-dp-recursion-on2-time-com-d8pc | \n/*\n\nSuppose we have \'m\' elements in one array and \'n\' elements in another and we need to arrange \'n\' elemnts around \'m\' elements of first array with | ashish10comp | NORMAL | 2020-11-11T05:38:09.141731+00:00 | 2020-11-11T05:42:02.636708+00:00 | 680 | false | ```\n/*\n\nSuppose we have \'m\' elements in one array and \'n\' elements in another and we need to arrange \'n\' elemnts around \'m\' elements of first array without breaking the order of elements in any of the arrays.\n\narray first = _ l1 _ l2 _ l3 _ ..... _ li-2 _ li-1 _ li _ li+1 _ ..... _ lm-2 _ lm-1 _ lm _\n\nar... | 2 | 0 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Simple C++ Solution | simple-c-solution-by-mayankdhiman-d8pl | \nclass Solution {\npublic:\n vector < vector < long long > > pascal;\n int MOD = 1e9 + 7;\n \n int numOfWays(vector<int>& nums) {\n int N = | mayankdhiman | NORMAL | 2020-08-30T18:51:57.290222+00:00 | 2020-08-30T18:51:57.290277+00:00 | 248 | false | ```\nclass Solution {\npublic:\n vector < vector < long long > > pascal;\n int MOD = 1e9 + 7;\n \n int numOfWays(vector<int>& nums) {\n int N = nums.size();\n pascal = vector < vector < long long > > (N + 1);\n for (int i = 0; i < N + 1; i ++){\n pascal[i] = vector < long lon... | 2 | 0 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | [Python] Simple code beats 100% in runtime with explanations | python-simple-code-beats-100-in-runtime-d4nz0 | Note: \nThe difficult part is to calculate 1000!\nThis solution is not the best one, but is very easy to come up with.\nIt is not good because number gets very | lichuan199010 | NORMAL | 2020-08-30T06:56:20.546482+00:00 | 2020-08-30T07:15:01.785244+00:00 | 232 | false | Note: \nThe difficult part is to calculate 1000!\nThis solution is not the best one, but is very easy to come up with.\nIt is not good because number gets very large as the number of nodes gets bigger.\nIdeally, we should use modular calculations.\n\nUsing pascal\'s triangle\uFF08\u6768\u8F89\u4E09\u89D2\uFF09to calcul... | 2 | 1 | [] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Python short and simple | python-short-and-simple-by-ritam777-wbsm | Simple recursion\nNumber of ways of re-arranging current array = (number of ways of rearranging m values of left subtree) * (number of ways of rearranging n val | ritam777 | NORMAL | 2020-08-30T04:17:06.041394+00:00 | 2020-08-30T04:24:12.455384+00:00 | 318 | false | **Simple recursion**\nNumber of ways of re-arranging current array = (number of ways of rearranging **m** values of left subtree) * (number of ways of rearranging **n** values of right subtree) * (number of ways of inter-leaving two arrays of sizes m and n having unique integers such that order is preserved i.e. **xCy ... | 2 | 1 | [] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | C++ Explanation | c-explanation-by-justinwongjianhui-1qed | Took me quite a while to get it, but the \nnumber of ways to arrange nums = ways to arrange the numbers in the left subtree * ways to arrange numbers in right s | justinwongjianhui | NORMAL | 2020-08-30T04:16:24.487844+00:00 | 2020-08-30T04:18:23.016294+00:00 | 688 | false | Took me quite a while to get it, but the \nnumber of ways to arrange nums = ways to arrange the numbers in the left subtree * ways to arrange numbers in right subtree * positions to fit the left/right subtree back into the original array\n\n```\nclass Solution {\npublic:\n int mod=1e9+7;\n long long power(long lo... | 2 | 0 | [] | 2 |
number-of-ways-to-reorder-array-to-get-same-bst | Beats 94.59%of users with JavaScript | beats-9459of-users-with-javascript-by-ss-kke8 | \n# Code\n\nvar numOfWays = function(nums) {\n const x = numOfWaysHelper(nums) - 1n;\n return x % 1_000_000_007n;\n}\nvar numOfWaysHelper = function(nums) | SSDeepakReddy | NORMAL | 2023-10-14T05:35:48.098980+00:00 | 2023-10-14T05:35:48.099004+00:00 | 26 | false | \n# Code\n```\nvar numOfWays = function(nums) {\n const x = numOfWaysHelper(nums) - 1n;\n return x % 1_000_000_007n;\n}\nvar numOfWaysHelper = function(nums) {\n if(nums.length < 3)\n return 1n;\n \n const root = nums[0];\n const left = nums.filter(p => p < root);\n const right = nums.filt... | 1 | 0 | ['JavaScript'] | 0 |
number-of-ways-to-reorder-array-to-get-same-bst | Combinations + DFS | C++ | combinations-dfs-c-by-tusharbhart-a8pu | \nclass Solution {\n int mod = 1e9 + 7;\n long dfs(vector<int> &nums, vector<vector<int>> &ncr) {\n int n = nums.size();\n if(n <= 2) return | TusharBhart | NORMAL | 2023-06-30T19:58:28.797221+00:00 | 2023-06-30T19:58:28.797244+00:00 | 22 | false | ```\nclass Solution {\n int mod = 1e9 + 7;\n long dfs(vector<int> &nums, vector<vector<int>> &ncr) {\n int n = nums.size();\n if(n <= 2) return 1;\n\n vector<int> l, r;\n for(int i=1; i<n; i++) {\n if(nums[i] < nums[0]) l.push_back(nums[i]);\n else r.push_back(num... | 1 | 0 | ['Recursion', 'Combinatorics', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.