Skip to content

Commit 66f2431

Browse files
committed
🚀 25-Jun-2020
1 parent c697b74 commit 66f2431

7 files changed

+660
-0
lines changed

Diff for: array/inversion_count_merge_sort.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Given an array of positive integers. The task is to find inversion count of array.
3+
4+
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
5+
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
6+
7+
Input:
8+
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, the size of array. The second line of each test case contains N elements.
9+
10+
Output:
11+
Print the inversion count of array.
12+
13+
Constraints:
14+
1 ≤ T ≤ 100
15+
1 ≤ N ≤ 107
16+
1 ≤ C ≤ 1018
17+
18+
Example:
19+
Input:
20+
1
21+
5
22+
2 4 1 3 5
23+
24+
Output:
25+
3
26+
27+
Explanation:
28+
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
29+
*/
30+
31+
32+
33+
34+
35+
36+
37+
38+
#include<bits/stdc++.h>
39+
using namespace std;
40+
41+
long long invCount;
42+
43+
void merge(vector<long long> &v, long long l, long long mid, long long r){
44+
long long n1=mid-l+1;
45+
long long n2=r-(mid+1)+1;
46+
vector<long long> L(n1);
47+
vector<long long> R(n2);
48+
49+
for(long long i=0;i<n1;i++)
50+
L[i]=v[l+i];
51+
for(long long j=0;j<n2;j++)
52+
R[j]=v[mid+1+j];
53+
54+
long long i=0, j=0, k=l;
55+
while(i<n1 && j<n2){
56+
if(L[i]<=R[j]){
57+
v[k]=L[i];
58+
i++;
59+
} else {
60+
v[k]=R[j];
61+
j++;
62+
invCount+=n1-i;
63+
}
64+
k++;
65+
}
66+
67+
while(i<n1){
68+
v[k]=L[i];
69+
i++;
70+
k++;
71+
}
72+
73+
while(j<n2){
74+
v[k]=R[j];
75+
j++;
76+
k++;
77+
}
78+
}
79+
80+
void mergeSort(vector<long long> &v, long long l, long long r){
81+
if(l<r){
82+
long long mid=l+(r-l)/2;
83+
mergeSort(v, l, mid);
84+
mergeSort(v, mid+1, r);
85+
merge(v, l, mid, r);
86+
}
87+
}
88+
89+
int main(){
90+
ios_base::sync_with_stdio(false);
91+
cin.tie(NULL);
92+
cout.tie(NULL);
93+
int t;
94+
cin>>t;
95+
while(t--){
96+
long long n;
97+
cin>>n;
98+
vector<long long> v;
99+
100+
for(long long i=0;i<n;i++){
101+
long long ch;
102+
cin>>ch;
103+
v.push_back(ch);
104+
}
105+
106+
invCount=0;
107+
mergeSort(v, 0, n-1);
108+
cout<<invCount<<endl;
109+
}
110+
111+
return 0;
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
2+
3+
Example:
4+
5+
Input: 3
6+
Output: 5
7+
Explanation:
8+
Given n = 3, there are a total of 5 unique BST's:
9+
10+
1 3 3 2 1
11+
\ / / / \ \
12+
3 2 1 1 3 2
13+
/ / \ \
14+
2 1 2 3
15+
16+
17+
18+
19+
20+
21+
22+
23+
class Solution {
24+
public:
25+
26+
long long gcdd(long long a, long long b){
27+
if(a==0) return b;
28+
return gcdd(b%a, a);
29+
}
30+
31+
long long catalan(int n, int r){
32+
if(n==0) return 1;
33+
if(r==0) return 1;
34+
long long p=1, k=1;
35+
if(n-r<r) r=n-r;
36+
while(r){
37+
p*=n;
38+
k*=r;
39+
int gcd=gcdd(p, k);
40+
p/=gcd;
41+
k/=gcd;
42+
n--;
43+
r--;
44+
}
45+
return p/k;
46+
}
47+
48+
int numTrees(int x) {
49+
if(x<=0) return 1;
50+
return catalan(2*x, x)/(x+1);
51+
}
52+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
We have some permutation A of [0, 1, ..., N - 1], where N is the length of A.
2+
3+
The number of (global) inversions is the number of i < j with 0 <= i < j < N and A[i] > A[j].
4+
5+
The number of local inversions is the number of i with 0 <= i < N and A[i] > A[i+1].
6+
7+
Return true if and only if the number of global inversions is equal to the number of local inversions.
8+
9+
Example 1:
10+
11+
Input: A = [1,0,2]
12+
Output: true
13+
Explanation: There is 1 global inversion, and 1 local inversion.
14+
Example 2:
15+
16+
Input: A = [1,2,0]
17+
Output: false
18+
Explanation: There are 2 global inversions, and 1 local inversion.
19+
Note:
20+
21+
A will be a permutation of [0, 1, ..., A.length - 1].
22+
A will have length in range [1, 5000].
23+
The time limit for this problem has been reduced.
24+
25+
26+
27+
28+
29+
30+
31+
32+
class Solution {
33+
public:
34+
int localInv=0, globalInv=0;
35+
36+
37+
void merge(vector<int> &v, int l, int mid, int r){
38+
int n1=mid-l+1;
39+
int n2=r-(mid+1)+1;
40+
vector<int> L(n1);
41+
vector<int> R(n2);
42+
43+
for(int i=0;i<n1;i++)
44+
L[i]=v[l+i];
45+
for(int j=0;j<n2;j++)
46+
R[j]=v[mid+1+j];
47+
48+
int i=0, j=0, k=l;
49+
while(i<n1 && j<n2){
50+
if(L[i]<=R[j]){
51+
v[k]=L[i];
52+
i++;
53+
} else {
54+
v[k]=R[j];
55+
j++;
56+
globalInv+=n1-i;
57+
}
58+
k++;
59+
}
60+
61+
while(i<n1){
62+
v[k]=L[i];
63+
i++;
64+
k++;
65+
}
66+
67+
while(j<n2){
68+
v[k]=R[j];
69+
j++;
70+
k++;
71+
}
72+
}
73+
74+
void mergeSort(vector<int> &v, int l, int r){
75+
if(l<r){
76+
int mid=l+(r-l)/2;
77+
mergeSort(v, l, mid);
78+
mergeSort(v, mid+1, r);
79+
merge(v, l, mid, r);
80+
}
81+
}
82+
83+
bool isIdealPermutation(vector<int>& v) {
84+
85+
// cal local inv
86+
int n=v.size();
87+
for(int i=0;i<n-1;i++){
88+
if(v[i]>v[i+1]) localInv++;
89+
}
90+
91+
// cal global inv
92+
93+
mergeSort(v, 0, n-1);
94+
95+
if(localInv==globalInv) return true;
96+
else return false;
97+
98+
}
99+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
2+
3+
Example:
4+
5+
Input: 3
6+
Output: 5
7+
Explanation:
8+
Given n = 3, there are a total of 5 unique BST's:
9+
10+
1 3 3 2 1
11+
\ / / / \ \
12+
3 2 1 1 3 2
13+
/ / \ \
14+
2 1 2 3
15+
16+
17+
18+
19+
20+
21+
22+
23+
class Solution {
24+
public:
25+
26+
long long gcdd(long long a, long long b){
27+
if(a==0) return b;
28+
return gcdd(b%a, a);
29+
}
30+
31+
long long catalan(int n, int r){
32+
if(n==0) return 1;
33+
if(r==0) return 1;
34+
long long p=1, k=1;
35+
if(n-r<r) r=n-r;
36+
while(r){
37+
p*=n;
38+
k*=r;
39+
int gcd=gcdd(p, k);
40+
p/=gcd;
41+
k/=gcd;
42+
n--;
43+
r--;
44+
}
45+
return p/k;
46+
}
47+
48+
int numTrees(int x) {
49+
if(x<=0) return 1;
50+
return catalan(2*x, x)/(x+1);
51+
}
52+
};

0 commit comments

Comments
 (0)