Skip to content

Commit ccf675c

Browse files
committed
Added C++ Solution
1 parent fb2d646 commit ccf675c

File tree

12 files changed

+593
-0
lines changed

12 files changed

+593
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
@author Farheen Bano
3+
4+
Date 13-08-2021
5+
6+
Reference-
7+
https://leetcode.com/problems/combination-sum-ii/
8+
*/
9+
10+
class Solution {
11+
public:
12+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
13+
vector<vector<int>> result;
14+
vector<int> cur;
15+
sort(candidates.begin(),candidates.end());
16+
generateSubsets(0,candidates,cur,result,target);
17+
return result;
18+
}
19+
20+
void generateSubsets(int index, vector<int>& candidates, vector<int>& cur, vector<vector<int>>& result, int target){
21+
if(target==0){
22+
result.push_back(cur);
23+
return;
24+
}
25+
26+
if(target<0)
27+
return;
28+
29+
for(int i=index;i<candidates.size();i++){
30+
if(i>index && candidates[i]==candidates[i-1])
31+
continue;
32+
cur.push_back(candidates[i]);
33+
generateSubsets(i+1,candidates,cur,result,target-candidates[i]);
34+
cur.pop_back();
35+
}
36+
}
37+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
@author Farheen Bano
3+
4+
0/1 Knapsack Problem - Given items of certain weights/values and maximum allowed weight
5+
how to pick items to pick items from this set to maximize sum of value of items such that
6+
sum of weights is less than or equal to maximum allowed weight.
7+
8+
Time complexity - O(W*total items)
9+
10+
Reference-
11+
https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
12+
*/
13+
14+
//------------------------------------------------------------------------------
15+
//Memoization Approach
16+
17+
class Solution
18+
{
19+
public:
20+
int dp[1001][1001];
21+
//Function to return max value that can be put in knapsack of capacity W.
22+
int knapSack(int W, int wt[], int val[], int N) {
23+
memset(dp,-1,sizeof(dp));
24+
KS(W,wt,val,N);
25+
}
26+
27+
//Recursive Function
28+
int KS(int W, int wt[], int val[], int N){
29+
if(N==0 || W==0)
30+
return 0;
31+
32+
if(dp[N][W]!=-1){
33+
return dp[N][W];
34+
}
35+
36+
if(wt[N-1]<=W){
37+
dp[N][W]=max(
38+
(val[N-1]+KS(W-wt[N-1],wt,val,N-1)),
39+
KS(W,wt,val,N-1)
40+
);
41+
return dp[N][W];
42+
}
43+
dp[N][W]=KS(W,wt,val,N-1);
44+
return dp[N][W];
45+
}
46+
};
47+
48+
//------------------------------------------------------------------------------
49+
//Tabulation Approach
50+
51+
class Solution
52+
{
53+
public:
54+
//Function to return max value that can be put in knapsack of capacity W.
55+
int knapSack(int W, int wt[], int val[], int N){
56+
int dp[N+1][W+1];
57+
58+
for(int i=0;i<N+1;i++){
59+
for(int j=0;j<W+1;j++){
60+
if(i==0||j==0){
61+
dp[i][j]=0;
62+
}
63+
else if(wt[i-1]<=j){
64+
dp[i][j]=max(val[i-1]+dp[i-1][j-wt[i-1]],dp[i-1][j]);
65+
}
66+
else
67+
dp[i][j]=dp[i-1][j];
68+
}
69+
}
70+
return dp[N][W];
71+
}
72+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
@author Farheen Bano
3+
4+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5+
6+
Date 14-08-2021
7+
8+
References-
9+
https://leetcode.com/problems/climbing-stairs/
10+
https://www.geeksforgeeks.org/count-ways-reach-nth-stair/
11+
12+
NOTE: Variation of Fibinacci Series
13+
*/
14+
15+
//Recursive Solution
16+
//Result in TLE error
17+
18+
class Solution {
19+
public int climbStairs(int n) {
20+
return countWays(n+1);
21+
}
22+
23+
int countWays(int n){
24+
if (n <= 1)
25+
return n;
26+
return countWays(n - 1) + countWays(n - 2);
27+
}
28+
}
29+
30+
31+
//Dynamic Programming Solution
32+
33+
class Solution {
34+
public:
35+
int climbStairs(int n) {
36+
if (n == 1) {
37+
return 1;
38+
}
39+
int* dp = new int[n + 1];
40+
dp[1] = 1;
41+
dp[2] = 2;
42+
for (int i = 3; i <= n; i++) {
43+
dp[i] = dp[i - 1] + dp[i - 2];
44+
}
45+
return dp[n];
46+
}
47+
};
48+
49+
50+
//Constant Space Solution
51+
52+
class Solution {
53+
public:
54+
int climbStairs(int n) {
55+
if(n==1)
56+
return 1;
57+
int a=1;
58+
int b=2;
59+
for(int i=3;i<=n;i++){
60+
int c=a+b;
61+
a=b;
62+
b=c;
63+
}
64+
return b;
65+
}
66+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
@author Farheen Bano
3+
4+
Date 14-08-2021
5+
6+
References-
7+
https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/
8+
https://leetcode.com/problems/longest-common-subsequence/
9+
*/
10+
11+
//------------------------------------------------------------------------------
12+
//Memoization Approach
13+
//TLE Problem
14+
15+
class Solution
16+
{
17+
public:
18+
int dp[1001][1001];
19+
//Function to find the length of longest common subsequence in two strings.
20+
int lcs(int x, int y, string s1, string s2) {
21+
memset(dp,-1,sizeof(dp));
22+
return subsequence(x, y, s1, s2);
23+
}
24+
25+
//Recursive Function
26+
int subsequence(int x, int y, string s1, string s2) {
27+
if(x==0 || y==0)
28+
return 0;
29+
30+
if(dp[x][y]!=-1)
31+
return dp[x][y];
32+
33+
if(s1[x-1]==s2[y-1]){
34+
dp[x][y]= 1+subsequence(x-1,y-1,s1,s2);
35+
return dp[x][y];
36+
}
37+
38+
dp[x][y]=max(subsequence(x-1,y,s1,s2),subsequence(x,y-1,s1,s2));
39+
return dp[x][y];
40+
}
41+
};
42+
43+
//------------------------------------------------------------------------------
44+
//Tabulation Approach
45+
46+
class Solution
47+
{
48+
public:
49+
//Function to find the length of longest common subsequence in two strings.
50+
int lcs(int x, int y, string s1, string s2)
51+
{
52+
int dp[x+1][y+1];
53+
54+
for(int i=0;i<=x;i++){
55+
for(int j=0;j<=y;j++){
56+
if(i==0 || j==0)
57+
dp[i][j]=0;
58+
else if(s1[i-1]==s2[j-1]){
59+
dp[i][j]=1+dp[i-1][j-1];
60+
}
61+
else
62+
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
63+
}
64+
}
65+
return dp[x][y];
66+
}
67+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
@author Farheen Bano
3+
4+
Date 14-08-2021
5+
6+
Reference-
7+
https://practice.geeksforgeeks.org/problems/longest-common-substring1452/1
8+
9+
NOTE: Variation of Longest Common Subsequence
10+
*/
11+
12+
//------------------------------------------------------------------------------
13+
//Tabulation Approach
14+
15+
class Solution{
16+
public:
17+
18+
int longestCommonSubstr (string S1, string S2, int n, int m)
19+
{
20+
int dp[n+1][m+1];
21+
int max_len=0;
22+
23+
for(int i=0;i<=n;i++){
24+
for(int j=0;j<=m;j++){
25+
if(i==0 || j==0)
26+
dp[i][j]=0;
27+
else if(S1[i-1]==S2[j-1]){
28+
dp[i][j]=1+dp[i-1][j-1];
29+
max_len=max(max_len,dp[i][j]);
30+
}
31+
else
32+
dp[i][j]=0;
33+
}
34+
}
35+
return max_len;
36+
}
37+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
@author Farheen Bano
3+
4+
Date 14-08-2021
5+
6+
References-
7+
https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/
8+
https://leetcode.com/problems/longest-palindromic-subsequence/
9+
10+
NOTE: Variation of Longest Common Subsequence
11+
*/
12+
13+
//------------------------------------------------------------------------------
14+
//Tabulation Approach
15+
16+
class Solution {
17+
public:
18+
int longestPalindromeSubseq(string s) {
19+
string reverse_s=s;
20+
int m=s.size();
21+
reverse(reverse_s.begin(),reverse_s.end());
22+
return lcs(m,m,s,reverse_s);
23+
}
24+
25+
int lcs(int x, int y, string s1, string s2) {
26+
int dp[x+1][y+1];
27+
28+
for(int i=0;i<=x;i++){
29+
for(int j=0;j<=y;j++){
30+
if(i==0 || j==0)
31+
dp[i][j]=0;
32+
else if(s1[i-1]==s2[j-1]){
33+
dp[i][j]=1+dp[i-1][j-1];
34+
}
35+
else
36+
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
37+
}
38+
}
39+
return dp[x][y];
40+
}
41+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
@author Farheen Bano
3+
4+
Date 14-08-2021
5+
6+
Reference-
7+
https://www.geeksforgeeks.org/shortest-common-supersequence/
8+
9+
NOTE: Variation of Longest Common Subsequence
10+
*/
11+
12+
13+
//------------------------------------------------------------------------------
14+
//Tabulation Approach
15+
16+
class Solution
17+
{
18+
public:
19+
//Function to find length of shortest common supersequence of two strings.
20+
int shortestCommonSupersequence(string X, string Y, int m, int n){
21+
return n+m-lcs(m,n,X,Y);
22+
}
23+
24+
int lcs(int x, int y, string s1, string s2)
25+
{
26+
int dp[x+1][y+1];
27+
28+
for(int i=0;i<=x;i++){
29+
for(int j=0;j<=y;j++){
30+
if(i==0 || j==0)
31+
dp[i][j]=0;
32+
else if(s1[i-1]==s2[j-1]){
33+
dp[i][j]=1+dp[i-1][j-1];
34+
}
35+
else
36+
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
37+
}
38+
}
39+
return dp[x][y];
40+
}
41+
};

0 commit comments

Comments
 (0)