Skip to content

Commit f4c5142

Browse files
committed
Practise 12-Jun-2020
1 parent 56862c8 commit f4c5142

9 files changed

+635
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
2+
3+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
4+
5+
Note: You are not suppose to use the library's sort function for this problem.
6+
7+
Example:
8+
9+
Input: [2,0,2,1,1,0]
10+
Output: [0,0,1,1,2,2]
11+
Follow up:
12+
13+
A rather straight forward solution is a two-pass algorithm using counting sort.
14+
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
15+
Could you come up with a one-pass algorithm using only constant space?
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
void sortColors(vector<int>& nums) {
24+
int zeros=0, ones=0, twos=0;
25+
26+
for(auto x: nums){
27+
if(x==0) zeros++;
28+
else if(x==1) ones++;
29+
}
30+
31+
twos=nums.size()-zeros-ones;
32+
33+
for(int i=0;i<zeros;i++) nums[i]=0;
34+
for(int i=zeros;i<zeros+ones;i++) nums[i]=1;
35+
for(int i=zeros+ones;i<zeros+ones+twos;i++) nums[i]=2;
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
2+
3+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
4+
5+
Note: You are not suppose to use the library's sort function for this problem.
6+
7+
Example:
8+
9+
Input: [2,0,2,1,1,0]
10+
Output: [0,0,1,1,2,2]
11+
Follow up:
12+
13+
A rather straight forward solution is a two-pass algorithm using counting sort.
14+
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
15+
Could you come up with a one-pass algorithm using only constant space?
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
void sortColors(vector<int>& nums) {
24+
int zeros=0, ones=0, twos=0;
25+
26+
for(auto x: nums){
27+
if(x==0) zeros++;
28+
else if(x==1) ones++;
29+
}
30+
31+
twos=nums.size()-zeros-ones;
32+
33+
for(int i=0;i<zeros;i++) nums[i]=0;
34+
for(int i=zeros;i<zeros+ones;i++) nums[i]=1;
35+
for(int i=zeros+ones;i<zeros+ones+twos;i++) nums[i]=2;
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Given a string str, find length of the longest repeating subseequence such that the two subsequence don’t have same string character at same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string.
3+
4+
Input:
5+
6+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer N denoting the length of string str.
7+
8+
The second line of each test case contains the string str consisting only of lower case english alphabets.
9+
10+
Output:
11+
12+
Print the length of the longest repeating subsequence for each test case in a new line.
13+
14+
15+
Constraints:
16+
17+
1<= T <=100
18+
19+
1<= N <=1000
20+
21+
22+
Example:
23+
24+
Input:
25+
26+
2
27+
28+
3
29+
30+
abc
31+
32+
5
33+
34+
axxxy
35+
36+
Output:
37+
38+
0
39+
40+
2
41+
*/
42+
43+
44+
45+
46+
#include<bits/stdc++.h>
47+
using namespace std;
48+
49+
int LRS(string s1, string s2, int n, int m){
50+
int dp[n+1][m+1];
51+
52+
for(int i=0;i<n+1;i++) dp[i][0]=0;
53+
for(int j=0;j<m+1;j++) dp[0][j]=0;
54+
55+
for(int i=1;i<n+1;i++){
56+
for(int j=1;j<m+1;j++){
57+
if(s1[i-1]==s2[j-1] && i!=j)
58+
dp[i][j]=1 + dp[i-1][j-1];
59+
else dp[i][j]=max(dp[i][j-1], dp[i-1][j]);
60+
}
61+
}
62+
return dp[n][m];
63+
}
64+
65+
int main(){
66+
ios_base::sync_with_stdio(false);
67+
cin.tie(NULL);
68+
cout.tie(NULL);
69+
70+
int t;
71+
cin>>t;
72+
while(t--){
73+
int n;
74+
cin>>n;
75+
string s1,s2;
76+
cin>>s1;
77+
s2=s1;
78+
cout<<LRS(s1,s2,n,n)<<endl;
79+
}
80+
81+
return 0;
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. There are many options to multiply a chain of matrices because matrix multiplication is associative i.e. no matter how one parenthesize the product, the result will be the same.
3+
4+
Example:
5+
if you had four matrices A, B, C, and D, you would have:
6+
7+
(ABC)D = (AB)(CD) = A(BCD) = ....
8+
However, the order in which one parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency.
9+
10+
For example:
11+
12+
A: 10 × 30 matrix
13+
B : 30 × 5 matrix
14+
C : 5 × 60 matrix
15+
Then,
16+
(AB)C = (10×30×5) + (10×5×60)
17+
= 1500 + 3000
18+
= 4500 operations
19+
A(BC) = (30×5×60) + (10×30×60)
20+
= 9000 + 18000
21+
= 27000 operations.
22+
Given an array arr[] which represents the chain of matrices such that the ith matrix Ai is of dimension arr[i-1] x arr[i]. Your task is to write a function that should print the minimum number of multiplications needed to multiply the chain.
23+
24+
Input: p[] = {40, 20, 30, 10, 30}
25+
Output: 26000
26+
There are 4 matrices of dimensions 40x20,
27+
20x30, 30x10 and 10x30. Let the input 4
28+
matrices be A, B, C and D. The minimum
29+
number of multiplications are obtained
30+
by putting parenthesis in following way
31+
(A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30
32+
33+
Input: p[] = {10, 20, 30, 40, 30}
34+
Output: 30000
35+
There are 4 matrices of dimensions 10x20,
36+
20x30, 30x40 and 40x30. Let the input 4
37+
matrices be A, B, C and D. The minimum
38+
number of multiplications are obtained by
39+
putting parenthesis in following way
40+
((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30
41+
Input:
42+
The first line of the input contains an integer T, denoting the number of test cases. Then T test case follows. The first line of each test case contains an integer N, denoting the number of elements in the array.
43+
Then next line contains N space separated integers denoting the values of the element in the array.
44+
45+
Output:
46+
For each test case the print the minimum number of operations needed to multiply the chain.
47+
48+
Constraints:
49+
1<=T<=100
50+
2<=N<=100
51+
1<=A[]<=500
52+
53+
Example:
54+
Input:
55+
2
56+
5
57+
1 2 3 4 5
58+
3
59+
3 3 3
60+
Output:
61+
38
62+
27
63+
*/
64+
65+
66+
67+
68+
69+
#include<bits/stdc++.h>
70+
using namespace std;
71+
72+
int dp[102][102];
73+
74+
int MCM(int a[], int i, int j){
75+
if(i>=j) return 0;
76+
if(dp[i][j]!=-1) return dp[i][j];
77+
int mini=INT_MAX;
78+
for(int k=i;k<j;k++){
79+
int temp=MCM(a,i,k)+MCM(a,k+1,j)+(a[i-1]*a[k]*a[j]);
80+
if(temp<mini) mini=temp;
81+
}
82+
return dp[i][j]=mini;
83+
}
84+
85+
int main(){
86+
ios_base::sync_with_stdio(false);
87+
cin.tie(NULL);
88+
cout.tie(NULL);
89+
int t;
90+
cin>>t;
91+
while(t--){
92+
int n;
93+
cin>>n;
94+
int a[n];
95+
for(int i=0;i<n;i++) cin>>a[i];
96+
memset(dp, -1, sizeof(dp));
97+
cout<<MCM(a,1,n-1)<<endl;
98+
}
99+
100+
return 0;
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. There are many options to multiply a chain of matrices because matrix multiplication is associative i.e. no matter how one parenthesize the product, the result will be the same.
3+
4+
Example:
5+
if you had four matrices A, B, C, and D, you would have:
6+
7+
(ABC)D = (AB)(CD) = A(BCD) = ....
8+
However, the order in which one parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency.
9+
10+
For example:
11+
12+
A: 10 × 30 matrix
13+
B : 30 × 5 matrix
14+
C : 5 × 60 matrix
15+
Then,
16+
(AB)C = (10×30×5) + (10×5×60)
17+
= 1500 + 3000
18+
= 4500 operations
19+
A(BC) = (30×5×60) + (10×30×60)
20+
= 9000 + 18000
21+
= 27000 operations.
22+
Given an array arr[] which represents the chain of matrices such that the ith matrix Ai is of dimension arr[i-1] x arr[i]. Your task is to write a function that should print the minimum number of multiplications needed to multiply the chain.
23+
24+
Input: p[] = {40, 20, 30, 10, 30}
25+
Output: 26000
26+
There are 4 matrices of dimensions 40x20,
27+
20x30, 30x10 and 10x30. Let the input 4
28+
matrices be A, B, C and D. The minimum
29+
number of multiplications are obtained
30+
by putting parenthesis in following way
31+
(A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30
32+
33+
Input: p[] = {10, 20, 30, 40, 30}
34+
Output: 30000
35+
There are 4 matrices of dimensions 10x20,
36+
20x30, 30x40 and 40x30. Let the input 4
37+
matrices be A, B, C and D. The minimum
38+
number of multiplications are obtained by
39+
putting parenthesis in following way
40+
((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30
41+
Input:
42+
The first line of the input contains an integer T, denoting the number of test cases. Then T test case follows. The first line of each test case contains an integer N, denoting the number of elements in the array.
43+
Then next line contains N space separated integers denoting the values of the element in the array.
44+
45+
Output:
46+
For each test case the print the minimum number of operations needed to multiply the chain.
47+
48+
Constraints:
49+
1<=T<=100
50+
2<=N<=100
51+
1<=A[]<=500
52+
53+
Example:
54+
Input:
55+
2
56+
5
57+
1 2 3 4 5
58+
3
59+
3 3 3
60+
Output:
61+
38
62+
27
63+
*/
64+
65+
66+
67+
68+
69+
70+
#include<bits/stdc++.h>
71+
using namespace std;
72+
73+
int MCM(int a[], int i, int j){
74+
if(i>=j) return 0;
75+
int mini=INT_MAX;
76+
for(int k=i;k<j;k++){
77+
int temp=MCM(a,i,k) + MCM(a,k+1,j) + (a[i-1]*a[k]*a[j]);
78+
if(temp<mini) mini=temp;
79+
}
80+
return mini;
81+
}
82+
83+
int main(){
84+
ios_base::sync_with_stdio(false);
85+
cin.tie(NULL);
86+
cout.tie(NULL);
87+
int t;
88+
cin>>t;
89+
while(t--){
90+
int n;
91+
cin>>n;
92+
int a[n];
93+
for(int i=0;i<n;i++) cin>>a[i];
94+
cout<<MCM(a,1,n-1)<<endl;
95+
}
96+
97+
return 0;
98+
}

0 commit comments

Comments
 (0)