Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.
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
62 changes: 62 additions & 0 deletions Dynamic-Programming/0-1Knapsack(DP).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Space Complexity : O(n^2)
Time Complexity : O(n^2)

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int w;
cin>>w;
int p[n+1];
p[0] = 0;
int wt[n+1];
wt[0] = 0;
for(int i=1;i<n+1;i++)
{
cin>>p[i];
}
for(int i=1;i<n+1;i++)
{
cin>>wt[i];
}
/*for(int i=1;i<n+1;i++)
cout<<p[i]<<" ";
cout<<endl;
for(int i=1;i<n+1;i++)
cout<<wt[i]<<" ";*/
int m[n+1][w+1];
for(int i=0;i<n+1;i++)
{
for(int j=0;j<w+1;j++)
{
if(i==0 || j==0)
{
m[i][j]=0;
}
else if(wt[i]<=j)
{
m[i][j] = max(p[i] + m[i-1][j-wt[i]] , m[i-1][j]);
}
else
{
m[i][j] = m[i-1][j];
}
}
}
/*for(int i=0;i<n+1;i++)
{
for(int j=0;j<w+1;j++)
cout<<m[i][j];
cout<<endl;
}*/
cout<<m[n][w]<<endl;
}
return 0;
}
54 changes: 54 additions & 0 deletions Dynamic-Programming/EditDistance(DP).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*The idea is if the character is same then print the diagonal element
in that dp matrix else find the minimum of previous columm,row and diagonal
element and set minimum at the current position in dp matirx and at last
print the last row and column value as it will give us the minimum*/

Space Complexity : O(n)
Time Complexity : O(n^2)

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int minimum(int a,int b,int c)
{
int result = min(min(a,b),c);
return result;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int p,q;
cin>>p>>q;
char s1[p];
char s2[q];
cin>>s1>>s2;
//cout<<s1<<" "<<s2;
int dp[p+1][q+1];
for(int i=0;i<p+1;i++)
{
for(int j=0;j<q+1;j++)
{

if(i == 0)
dp[i][j] = j;

else if(j == 0)
dp[i][j] = i;

else if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1];

else
{
dp[i][j] = 1 + minimum(dp[i-1][j-1],dp[i-1][j],dp[i][j-1]);
}

}
}
cout<<dp[p][q]<<endl;
}
return 0;
}
55 changes: 55 additions & 0 deletions Sorting/Countsort.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Count Sort
Space Complexity : O(max) // max = maximum element
Time Complexity : O(n)

#include<iostream.h>
#include<conio.h>
#define max 20
int a[max];
void getdata(int n)
{cout<<"enter elements";
int i;
for(i=0;i<n;i++)
{cin>>a[i];
}

}
int maxi(int n)
{int i,j,jl;
for(i=0;i<1;i++)
{jl=a[i];
for(j=1;j<n;j++)
{if(jl<a[j])
jl=a[j];
}
}
return jl;
}
int main()
{clrscr();
int b,i,n,j;
cout<<"enter number of elements";
cin>>n;
getdata(n);
b=maxi(n);
int *p=new int[b+1];
for(i=0;i<b+1;i++)
{p[i]=0;
}
for(i=0;i<n;i++)
{p[a[i]]++;
}
i=0,j=0;
while(j<max+1)
{if(p[j]>0)
{a[i]=j;
i++;
p[j]--;
}
else j++;
}
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
return 0;
}