-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from SaiManasaPappu/master
* https://www.geeksforgeeks.org/maximum-sum-path-across-two-arrays/ * https://www.geeksforgeeks.org/coin-change-dp-7/ | https://www.geeksforgeeks.org/understanding-the-coin-change-problem-with-dynamic-programming/ * https://www.geeksforgeeks.org/minimum-positive-points-to-reach-destination/
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// For problem statement and I/O Format, visit: https://practice.geeksforgeeks.org/problems/maximum-sum-path-in-two-arrays/0 | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
int main() | ||
{ | ||
int test,m,n; | ||
cin>>test; | ||
while(test--){ | ||
cin>>n>>m; | ||
int a1[n], a2[m]; | ||
for(int i=0;i<n;i++) | ||
cin>>a1[i]; | ||
for(int i=0;i<m;i++) | ||
cin>>a2[i]; | ||
int count=0,i=0,j=0,sum1=0,sum2=0; | ||
while(i<n && j<m){ | ||
if(a1[i]==a2[j]){ | ||
while (i < n && j < m && a1[i] == a2[j]){ | ||
count = count + a1[i++]; | ||
j++; | ||
} | ||
count += max(sum1,sum2); | ||
sum1 = 0; | ||
sum2 = 0; | ||
} | ||
else if(a1[i]<a2[j]){ | ||
sum1+=a1[i]; | ||
i++; | ||
} | ||
else if(a1[i]>a2[j]){ | ||
sum2+=a2[j]; | ||
j++; | ||
} | ||
} | ||
if(i<n || j<m){ | ||
if(i<n){ | ||
while(i<n){ | ||
sum1+=a1[i++]; | ||
} | ||
} | ||
if(j<m){ | ||
while(j<m){ | ||
sum2+=a2[j++]; | ||
} | ||
} | ||
count += max(sum1,sum2); | ||
} | ||
cout<<count<<endl; | ||
} | ||
//code | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include<iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
int test,n,amount; | ||
cin>>test; | ||
while(test--){ | ||
cin>>amount>>n; | ||
int coin[n]; | ||
for(int i=0;i<n;i++) | ||
cin>>coin[i]; | ||
int am = amount+1; | ||
int a[n][am]; | ||
|
||
for(int i=0;i<n;i++) | ||
a[i][0]=0; | ||
|
||
for(int i=1;i<am;i++) | ||
a[0][i] = (i%coin[0]==0 ? i/coin[0] : -1 ); | ||
|
||
for(int i=1;i<n;i++){ | ||
for(int j=1;j<am;j++){ | ||
|
||
if(j<coin[i]) | ||
a[i][j] = a[i-1][j] ; | ||
|
||
else if(j==coin[i]) | ||
a[i][j]=1; | ||
|
||
else{ | ||
|
||
if(a[i-1][j] == -1 && a[i][j-coin[i]] == -1) | ||
a[i][j] = -1; | ||
|
||
else if(a[i-1][j] == -1) | ||
a[i][j] = 1 + a[i][j-coin[i]] ; | ||
|
||
else if(a[i][j-coin[i]] == -1) | ||
a[i][j] = a[i-1][j]; | ||
|
||
else | ||
a[i][j] = min(1 + a[i][j-coin[i]] , a[i-1][j]); | ||
|
||
} | ||
} | ||
} | ||
cout<<a[n-1][am-1]<<endl; | ||
} | ||
return 0; | ||
} |
46 changes: 46 additions & 0 deletions
46
dynamic_programming/C++/minimum_pts_to_reach_destination.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Problem statement, I/O formats clearly explained: https://practice.geeksforgeeks.org/problems/minimum-points-to-reach-destination/0 | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int mini=INT_MAX,**arr; | ||
|
||
// arr is the main dp matrix | ||
|
||
void count(int r,int c,int cr,int cc,int csum,int locmin){ | ||
|
||
int newsum = csum+arr[cr][cc]; | ||
if(cr==r-1 && cc==c-1) | ||
mini = min(mini,min(locmin,newsum)>0?1:(1-min(locmin,newsum))); | ||
|
||
else if(cr<r-1 && cc<c-1){ | ||
count(r,c,cr+1,cc,csum+arr[cr][cc],min(locmin,csum+arr[cr][cc])); | ||
count(r,c,cr,cc+1,csum+arr[cr][cc],min(locmin,csum+arr[cr][cc])); | ||
} | ||
else if(cr<r-1) | ||
count(r,c,cr+1,cc,csum+arr[cr][cc],min(locmin,csum+arr[cr][cc])); | ||
|
||
else if(cc<c-1) | ||
count(r,c,cr,cc+1,csum+arr[cr][cc],min(locmin,csum+arr[cr][cc])); | ||
|
||
} | ||
int main() | ||
{ | ||
int test,r,c; | ||
cin>>test; | ||
while(test--){ | ||
mini=INT_MAX; | ||
cin>>r>>c; | ||
arr = (int **)malloc(r * sizeof(int *)); | ||
for (int i=0; i<r; i++) | ||
arr[i] = (int *)malloc(c * sizeof(int)); | ||
for(int i=0;i<r;i++) | ||
for(int j=0;j<c;j++) | ||
cin>>arr[i][j]; | ||
count(r,c,0,0,0,INT_MAX); | ||
cout<< mini << endl; | ||
|
||
} | ||
return 0; | ||
} | ||
|