Skip to content

Commit ec36d4a

Browse files
committed
Practise 10-Jun-2020
1 parent bd2f9b2 commit ec36d4a

9 files changed

+581
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Given a string s and a string t, check if s is subsequence of t.
2+
3+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
4+
5+
Follow up:
6+
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
7+
8+
Credits:
9+
Special thanks to @pbrother for adding this problem and creating all test cases.
10+
11+
12+
13+
Example 1:
14+
15+
Input: s = "abc", t = "ahbgdc"
16+
Output: true
17+
Example 2:
18+
19+
Input: s = "axc", t = "ahbgdc"
20+
Output: false
21+
22+
23+
Constraints:
24+
25+
0 <= s.length <= 100
26+
0 <= t.length <= 10^4
27+
Both strings consists only of lowercase characters.
28+
29+
30+
31+
32+
33+
34+
35+
class Solution {
36+
public:
37+
bool isSubsequence(string s, string t) {
38+
int tlen=t.length();
39+
int slen=s.length();
40+
41+
if(slen>tlen) return 0;
42+
43+
int j=0;
44+
45+
for(int i=0;i<tlen;i++){
46+
if(t[i]==s[j]){
47+
j++;
48+
if(j==slen) break;
49+
}
50+
}
51+
52+
if(j==slen) return true;
53+
else return false;
54+
}
55+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
2+
3+
Example 1:
4+
5+
Input: coins = [1, 2, 5], amount = 11
6+
Output: 3
7+
Explanation: 11 = 5 + 5 + 1
8+
Example 2:
9+
10+
Input: coins = [2], amount = 3
11+
Output: -1
12+
Note:
13+
You may assume that you have an infinite number of each kind of coin.
14+
15+
16+
17+
18+
class Solution {
19+
public:
20+
int coinChange(vector<int>& coins, int amt) {
21+
int n=coins.size();
22+
int dp[n+1][amt+1];
23+
24+
for(int i=0;i<n+1;i++) dp[i][0]=0;
25+
for(int j=1;j<amt+1;j++) dp[0][j]=INT_MAX-1;
26+
27+
for(int i=1;i<n+1;i++){
28+
for(int j=1;j<amt+1;j++){
29+
if(coins[i-1]<=j)
30+
dp[i][j]=min(dp[i][j-coins[i-1]]+1, dp[i-1][j]);
31+
else dp[i][j]=dp[i-1][j];
32+
}
33+
}
34+
35+
return dp[n][amt]==INT_MAX-1 ? -1: dp[n][amt];
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Given a string s and a string t, check if s is subsequence of t.
2+
3+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
4+
5+
Follow up:
6+
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
7+
8+
Credits:
9+
Special thanks to @pbrother for adding this problem and creating all test cases.
10+
11+
12+
13+
Example 1:
14+
15+
Input: s = "abc", t = "ahbgdc"
16+
Output: true
17+
Example 2:
18+
19+
Input: s = "axc", t = "ahbgdc"
20+
Output: false
21+
22+
23+
Constraints:
24+
25+
0 <= s.length <= 100
26+
0 <= t.length <= 10^4
27+
Both strings consists only of lowercase characters.
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
bool isSubsequence(string s, string t) {
40+
int tlen=t.length();
41+
int slen=s.length();
42+
43+
if(slen>tlen) return 0;
44+
45+
int j=0;
46+
47+
for(int i=0;i<tlen;i++){
48+
if(t[i]==s[j]){
49+
j++;
50+
if(j==slen) break;
51+
}
52+
}
53+
54+
if(j==slen) return true;
55+
else return false;
56+
}
57+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Given a value V. You have to make change for V cents, given that you have infinite supply of each of C{ C1, C2, .. , Cm} valued coins. Find the minimum number of coins to make the change.
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is V and N, V is the value of cents and N is the number of coins. The second line of each test case contains N input C[i], value of available coins.
6+
7+
Output:
8+
Print the minimum number of coins to make the change, if not possible print "-1".
9+
10+
Constraints:
11+
1 ≤ T ≤ 100
12+
1 ≤ V ≤ 106
13+
1 ≤ N ≤ 106
14+
1 ≤ C[i] ≤ 106
15+
16+
Example:
17+
Input:
18+
1
19+
7 2
20+
2 1
21+
22+
Output:
23+
4
24+
25+
Explanation :
26+
Testcase 1: We can use coin with value 2 three times, and coin with value 1 one times to change a total of 7.
27+
*/
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
#include<bits/stdc++.h>
38+
using namespace std;
39+
40+
41+
int minCoinChange(int coins[], int n, int amt){
42+
int dp[n+1][amt+1];
43+
44+
for(int i=0;i<n+1;i++) dp[i][0]=0;
45+
for(int j=1;j<amt+1;j++) dp[0][j]=INT_MAX-1;
46+
47+
for(int i=1;i<n+1;i++){
48+
for(int j=1;j<amt+1;j++){
49+
if(coins[i-1]<=j)
50+
dp[i][j]=min(dp[i][j-coins[i-1]]+1, dp[i-1][j]);
51+
else dp[i][j]=dp[i-1][j];
52+
}
53+
}
54+
55+
56+
57+
return dp[n][amt]==INT_MAX-1 ? -1: dp[n][amt];
58+
}
59+
60+
61+
int main(){
62+
ios_base::sync_with_stdio(false);
63+
cin.tie(NULL);
64+
cout.tie(NULL);
65+
66+
int t;
67+
cin>>t;
68+
while(t--){
69+
int v, n;
70+
cin>>v>>n;
71+
72+
int coins[n];
73+
for(int i=0;i<n;i++) cin>>coins[i];
74+
75+
cout<<minCoinChange(coins, n, v)<<endl;
76+
}
77+
78+
79+
return 0;
80+
}
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.
3+
4+
Input:
5+
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'M' denoting the size of array. The second line contains M space-separated integers A1, A2, ..., AN denoting the elements of the array. The third line contains an integer 'N' denoting the cents.
6+
7+
Output:
8+
Print number of possible ways to make change for N cents.
9+
10+
Constraints:
11+
1 ≤ T ≤ 50
12+
1 ≤ N ≤ 300
13+
1 ≤ A[i] ≤ 300
14+
15+
Example:
16+
Input:
17+
2
18+
3
19+
1 2 3
20+
4
21+
4
22+
2 5 3 6
23+
10
24+
25+
Output:
26+
4
27+
5
28+
29+
Explanation:
30+
Testcase 1: The possiblities are as such: {1, 1, 1, 1}, {1, 1, 2}, {1, 3}, {2, 2}.
31+
*/
32+
33+
34+
35+
36+
37+
38+
#include<bits/stdc++.h>
39+
using namespace std;
40+
41+
int coinChange(int coins[], int n, int amt){
42+
int dp[n+1][amt+1];
43+
44+
for(int i=0;i<n+1;i++) dp[i][0]=1;
45+
for(int j=1;j<amt+1;j++) dp[0][j]=0;
46+
47+
for(int i=1;i<n+1;i++){
48+
for(int j=1;j<amt+1;j++){
49+
if(coins[i-1]<=j)
50+
dp[i][j]=dp[i][j-coins[i-1]] + dp[i-1][j];
51+
else dp[i][j]=dp[i-1][j];
52+
}
53+
}
54+
return dp[n][amt];
55+
}
56+
57+
int main(){
58+
ios_base::sync_with_stdio(false);
59+
cin.tie(NULL);
60+
cout.tie(NULL);
61+
int t;
62+
cin>>t;
63+
while(t--){
64+
int n;
65+
cin>>n;
66+
67+
int coins[n];
68+
for(int i=0;i<n;i++) cin>>coins[i];
69+
70+
int amt;
71+
cin>>amt;
72+
73+
cout<<coinChange(coins, n, amt)<<endl;
74+
75+
}
76+
77+
78+
79+
return 0;
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Given two sequences, find the length of longest subsequence present in both of them. Both the strings are of uppercase.
3+
4+
Input:
5+
First line of the input contains no of test cases T,the T test cases follow.
6+
Each test case consist of 2 space separated integers A and B denoting the size of string str1 and str2 respectively
7+
The next two lines contains the 2 string str1 and str2 .
8+
9+
Output:
10+
For each test case print the length of longest common subsequence of the two strings .
11+
12+
Constraints:
13+
1<=T<=200
14+
1<=size(str1),size(str2)<=100
15+
16+
Example:
17+
Input:
18+
2
19+
6 6
20+
ABCDGH
21+
AEDFHR
22+
3 2
23+
ABC
24+
AC
25+
26+
Output:
27+
3
28+
2
29+
30+
Explanation
31+
LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
32+
33+
LCS of "ABC" and "AC" is "AC" of length 2
34+
*/
35+
36+
37+
38+
39+
40+
#include<bits/stdc++.h>
41+
using namespace std;
42+
43+
int dp[102][102]; // set according to constraints
44+
45+
int LCS(string s1, string s2, int n, int m){
46+
if(n==0 || m==0) return 0;
47+
if(dp[n][m]!=-1) return dp[n][m];
48+
if(s1[n-1]==s2[m-1])
49+
return dp[n][m]=1 + LCS(s1,s2,n-1,m-1);
50+
else return dp[n][m]=max(LCS(s1,s2,n,m-1), LCS(s1,s2,n-1,m));
51+
}
52+
53+
int main(){
54+
ios_base::sync_with_stdio(false);
55+
cin.tie(NULL);
56+
cout.tie(NULL);
57+
58+
int t;
59+
cin>>t;
60+
while(t--){
61+
int n,m;
62+
cin>>n>>m;
63+
string s1,s2;
64+
cin>>s1>>s2;
65+
memset(dp, -1, sizeof(dp));
66+
cout<<LCS(s1,s2,n,m)<<endl;
67+
}
68+
69+
return 0;
70+
}

0 commit comments

Comments
 (0)