Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added important_DP_algorithms #2530

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Leetcode/C++/Count_Derangements_DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*

Count Derangements!
(Permutation such that no element appears in its original position)


*/

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int main()
{

std::ios::sync_with_stdio(false);
int T;
cin>>T;
// cin.ignore(); must be there when using getline(cin, s)
while(T--)
{

int n;

cin>>n;

int dp[n+1];
memset(dp,0,sizeof(dp));

dp[0]=0;

dp[1]=0;

dp[2]=1;

for(int i=3;i<=n;i++) {
dp[i] = (i-1)*(dp[i-1]+dp[i-2]);
}


cout<<dp[n]<<endl;


}
return 0;
}
88 changes: 88 additions & 0 deletions Leetcode/C++/Count_Of_All_Palindromic_Substrings_DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int dp[1001][1001];

bool ispalin(string s, int i,int j) {


if(i>j) {
return 1;
}

if(dp[i][j]!=-1) {
return dp[i][j];
}


if(s[i]!=s[j]) {
return dp[i][j]=0;
}

return dp[i][j]=ispalin(s,i+1,j-1);

}



int countPalin(string s) {

memset(dp,-1,sizeof(dp));

int n=s.size();

int c=0;


for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(ispalin(s,i,j)) {
c++;
}
}
}

return c;
}


int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif


string s;

cin>>s;

cout<<countPalin(s);


return 0;
}
85 changes: 85 additions & 0 deletions Leetcode/C++/Count_Of_Longest_Increasing_Subsequences_DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif



int n;

cin>>n;

int a[n];

for(int i=0;i<n;i++) {
cin>>a[i];
}

int lis[n];
int dp[n];

for(int i=0;i<n;i++) {
dp[i]=1;
lis[i]=1;
}

for(int i=1;i<n;i++) {
for(int j=0;j<i;j++) {
if(a[i]>a[j]) {

if(lis[i]<=lis[j]) {
lis[i]=lis[j]+1;
dp[i]=dp[j];
}

else if(lis[i]==lis[j]+1) {
dp[i]+=dp[j];
}

}
}
}

int y=*max_element(lis,lis+n);

int c=0;

for(int i=0;i<n;i++) {
if(lis[i]==y) {
c+=dp[i];
}
}

cout<<c;


return 0;
}
62 changes: 62 additions & 0 deletions Leetcode/C++/Count_Of_String_Appears_As_A_Subsequence_DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends


// User function Template for C++

class Solution{
public:
int countWays(string S1, string S2){

int n=S1.size();
int m=S2.size();

int dp[n+1][m+1];
memset(dp,0,sizeof(dp));

for(int i=0;i<n+1;i++) {
dp[i][0]=1;
}

for(int j=0;j<m+1;j++) {
dp[0][j]=0;
}

dp[0][0]=1;


for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(S1[i-1]==S2[j-1]) {
dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
}
else {
dp[i][j]=dp[i-1][j];
}
}
}

return dp[n][m];


}
};

// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
string S1, S2;
cin>>S1;
cin>>S2;

Solution ob;
cout<<ob.countWays(S1, S2)<<endl;
}
return 0;
} // } Driver Code Ends
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int max_count(int a[],int n,int k) {


int dp[n+1][k+1];

memset(dp,0,sizeof(dp));

for(int i=1;i<=n;i++) {

for(int j=1;j<=k;j++) {

dp[i][j]+=dp[i-1][j];

if(a[i-1]<=j&&a[i-1]>0) {
dp[i][j]+=dp[i-1][j/a[i-1]]+1;
}
}

}

return dp[n][k];
}



int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif


int n,k;

cin>>n>>k;

int a[n];

for(int i=0;i<n;i++) {
cin>>a[i];
}

cout<<max_count(a,n,k);


return 0;
}
Loading