Skip to content

Commit

Permalink
Merge pull request #36 from Naman-1234/fourthproblem
Browse files Browse the repository at this point in the history
add dp problem minimum cost
  • Loading branch information
keshavagarwal17 authored Oct 2, 2021
2 parents d82e1f4 + 5d157b8 commit 445403c
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Count_Steps_To_one.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include<bits/stdc++.h>
using namespace std;

//This is brute force
int countMinStepsToOne(int n)
{
if(n<=1)
return 0;
int a=countMinStepsToOne(n-1)+1;
int b=INT_MAX;
if(n%2==0)
b=countMinStepsToOne(n/2)+1;
a=min(a,b);
int c=INT_MAX;
if(n%3==0)
c=countMinStepsToOne(n/3)+1;
a=min(a,c);
return a;
}

//This is memoization
int helper(int n,int *arr)
{
if(n==1)
return 0;
if(arr[n]!=-1)
return arr[n];
int a=helper(n-1,arr)+1;
int b=INT_MAX;
if(n%2==0)
b=helper(n/2,arr)+1;
a=min(a,b);
int c=INT_MAX;
if(n%3==0)
c=helper(n/3,arr)+1;
arr[n]=min(a,c);
return arr[n];
}
int countMinStepsToOne(int n)
{
int *arr=new int[n+1];
for(int i=0;i<n+1;i++)
arr[i]=-1;
return helper(n,arr);
}



//This is DP
int countStepsToOne(int n)
{
int *arr=new int[n+1];
arr[0]=0;
arr[1]=0;
arr[2]=1;
for(int i=3;i<=n;i++)
{
int a=arr[i-1];
int b=INT_MAX;
if(i%2==0)
b=arr[i/2];
a=min(a,b);
int c=INT_MAX;
if(i%3==0)
c=arr[i/3];
arr[i]=min(a,c)+1;
}
return arr[n];

}
145 changes: 145 additions & 0 deletions Edit_Distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@


/*
*!Brute force Time complexity:O(3^n)
*! DP SOLUTION: TIme:O(m*n) space:O(m*n)
*!SPace optimized: Time:O(m*n) space:(m)
*/
//Brute force
#include<bits/stdc++.h>
using namespace std;
int editDistance(string s1, string s2)
{

if(s1.empty())
return s2.size();
if(s2.empty())
return s1.size();

if(s1==s2)
return 0;

int answer=0;
if(s1[0]!=s2[0])
{
int a=editDistance(s1.substr(1),s2);
int b=editDistance(s1,s2.substr(1));
int c=editDistance(s1.substr(1),s2.substr(1));
answer=min(a,min(b,c))+1;
}
else
{
answer= editDistance(s1.substr(1),s2.substr(1));
}
return answer;

}

//Better brute force, is to avoid using substr method
int min(int x, int y, int z) { return min(min(x, y), z); }

int editDist(string str1, string str2, int m, int n)
{
if (m == 0)
return n;


if (n == 0)
return m;

if (str1[m - 1] == str2[n - 1])
return editDist(str1, str2, m - 1, n - 1);


return 1
+ min(editDist(str1, str2, m, n - 1), // Insert
editDist(str1, str2, m - 1, n), // Remove
editDist(str1, str2, m - 1,
n - 1) // Replace
);
}


//*Memoization


int helper(string s1,string s2,int **arr)
{
int m=s1.size();
int n=s2.size();
if(s1.empty())
return s2.size();
if(s2.empty())
return s1.size();
if(s1==s2)
return 0;
if(arr[m][n]!=-1)
return arr[m][n];
if(s1[0]!=s2[0])
{
int a,b,c;
a=helper(s1.substr(1),s2,arr);
b=helper(s1,s2.substr(1),arr);
c=helper(s1.substr(1),s2.substr(1),arr);
arr[m][n]= min(a,min(b,c))+1;
}
else
arr[m][n]=helper(s1.substr(1),s2.substr(1),arr);
return arr[m][n];
}


int editDistance(string s1, string s2)
{
int m=s1.size();
int n=s2.size();
int i,j;
int **arr=new int*[m+1];
for(i=0;i<=m;i++)
arr[i]=new int[n+1];
for(i=0;i<=m;i++)
for(j=0;j<=n;j++)
arr[i][j]=-1;
return helper(s1,s2,arr);

}





// * Solution through DP

int editDistance(string s, string t) {
int m,n,i,j;
m=s.size();
n=t.size();
vector<vector<int>> dp(m+1,vector<int>(n+1));


dp[0][0]=0;

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

for(i=1;i<=m;i++)
dp[i][0]=i;

for(i=1;i<=m;i++){
for(j=1;j<=n;j++){

if(s[i-1]==t[j-1])
dp[i][j]=dp[i-1][j-1];
else{
int a = dp[i-1][j];
int b = dp[i][j-1];
int c = dp[i-1][j-1];
dp[i][j]=min(a,min(b,c))+1;
}
}
}
return dp[m][n];
}


108 changes: 108 additions & 0 deletions Minimum_Cost.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

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

int helper(int **input,int m,int n,int i,int j)
{
if(i>=m||j>=n)
return INT_MAX;
if(i==m-1&&j==n-1)
return input[i][j];
int a1=helper(input,m,n,i,j+1);
int a2=helper(input,m,n,i+1,j);
int a3=helper(input,m,n,i+1,j+1);
return min(a1,min(a2,a3))+input[i][j];
}



int minCostPath(int **input, int m, int n)
{
return helper(input,m,n,0,0);
}



// This is memoization



int helper(int **input,int m,int n,int i,int j,int **arr)
{
if(i>=m||j>=n)
return INT_MAX;
if(i==m-1&&j==n-1)
return input[i][j];
if(arr[i][j]!=-1)
return arr[i][j];
int a1=helper(input,m,n,i,j+1,arr);
int a2=helper(input,m,n,i+1,j,arr);
int a3=helper(input,m,n,i+1,j+1,arr);
arr[i][j]=min(a1,min(a2,a3))+input[i][j];
return arr[i][j];
}


-
int minCostPath(int **input, int m, int n)
{
int **arr;
arr = new int *[m+1];
for (int i = 0; i <= m; i++)
{
arr[i] = new int[n+1];
}
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
arr[i][j]=-1;
}
return helper(input,m,n,0,0,arr);
}


//Now taking the solution through DP


int minCostPath(int **input,int m,int n)
{
int **arr;
arr=new int*[m+1];
for(int i=0;i<=m;i++)
arr[i]=new int[n+1];
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
arr[i][j]=INT_MAX;
}
arr[m][n]=input[m-1][n-1];
for(int i=m;i>=1;i--)
{
for(int j=n;j>=1;j--)
{
int a,b,c;
a=b=c=INT_MAX;
//Initially took them as infinity, As infinity represents the index is not valid,so u cannot go there, and its value will be updated if we can go to a particular index
if(i+1<=m&&j+1<=n)
{
a=arr[i+1][j+1];
b=arr[i][j+1];
c=arr[i+1][j];
}
else if(i+1<=m)
{
c=arr[i+1][j];
}
else if (j+1<=n)
{
b=arr[i][j+1];
}
//Reason of this if statement is that we are initializing arr[m][n] and we don't want to initialize again,further if we do it then we will get wrong answer,bcoz this is the value which we are using for other ones, but if we try to initialize it then there is no value to use for it,SO a,b,c all will be INT_MAX
if(i==m&&j==n)
continue;
arr[i][j]=min(a,min(b,c))+input[i-1][j-1];
}
}
return arr[1][1];

}
39 changes: 39 additions & 0 deletions staircase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

//Using Memoization
long helper(int n,long *arr)
{
if(n<=1)
return 1;
if(n==2)
return 2;
if(arr[n]!=-1)
return arr[n];
arr[n]= helper(n-1,arr)+helper(n-2,arr)+helper(n-3,arr);
return arr[n];
}




long staircase(int n)
{
long *arr=new long[n+1];
for(int i=0;i<=n;i++)
arr[i]=-1;
return helper(n,arr);

}


//Using DP
long staircase(int n)
{
long *arr=new long[n+1];
arr[0]=arr[1]=1;
arr[2]=2;
for(int i=3;i<=n;i++)
{
arr[i]=arr[i-1]+arr[i-2]+arr[i-3];
}
return arr[n];
}

0 comments on commit 445403c

Please sign in to comment.