Skip to content

Dynamic Programming #1

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

Merged
merged 1 commit into from
Oct 4, 2019
Merged
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
113 changes: 113 additions & 0 deletions Dynamic Programming/LCS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <iomanip>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
using namespace std;

#define MP make_pair
#define PB push_back
#define ARR_MAX (int)1e5 //Max array length
#define INF (int)1e9 //10^9
#define EPS 1e-9 //10^-9
#define MOD 1000000007 //10^9+7
#define PI 3.1415926535897932384626433832795
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<Pii> VPii;
typedef vector<Vi> VVi;
typedef map<int,int> Mii;
typedef set<int> Si;
typedef multimap<int,int> MMii;
typedef multiset<int> MSi;
typedef unordered_map<int,int> UMii;
typedef unordered_set<int> USi;
typedef unordered_multimap<int,int> UMMii;
typedef unordered_multiset<int> UMSi;
typedef priority_queue<int> PQi;
typedef queue<int> Qi;
typedef deque<int> DQi;


string s1, s2;

int bruteForce(int i, int j){
if(i==s1.size() || j==s2.size()) return 0;
if(s1[i] == s2[j]) return 1+bruteForce(i+1, j+1);
return max(bruteForce(i+1, j), bruteForce(i, j+1));
}

VVi DP;
int dpTopDown(int i, int j){
if(i==s1.size() || j==s2.size()) return 0;
if(DP[i][j] != -1) return DP[i][j];
if(s1[i] == s2[j]) return DP[i][j] = 1+dpTopDown(i+1, j+1);
return DP[i][j] = max(dpTopDown(i+1, j), dpTopDown(i, j+1));
}

int dpBottomUp(){

for (int i = 0; i < s1.size(); i++) DP[i][0] = (int)(s1[i] == s2[0]);
for (int j = 0; j < s2.size(); j++) DP[0][j] = (int)(s1[0] == s2[j]);


for (int i = 1; i < s1.size(); i++)
{
for (int j = 1; j < s2.size(); j++)
{
int val = 0;
if(s1[i] == s2[j]) val = 1;
DP[i][j] = max(DP[i-1][j-1]+val, max(DP[i-1][j], DP[i][j-1]));
}
}

return DP[s1.size()-1][s2.size()-1];
}

void print() {
std::for_each(std::begin(DP), std::end(DP), [](Vi v) {std::for_each(std::begin(v), std::end(v), [](int a) {cout << a << ' ';});cout << endl; });cout << endl;
}
int main (int argc, char const *argv[]) {
s1 = "Hello";
s2 = "World is Hell but hey Hello";

cout << bruteForce(0,0) << endl;

DP = VVi(s1.size(), Vi(s2.size(), -1));
cout << dpTopDown(0,0) << endl;

print();

DP = VVi(s1.size(), Vi(s2.size(), 0));
cout << dpBottomUp() << endl;

print();

return EXIT_SUCCESS;
}
119 changes: 119 additions & 0 deletions Dynamic Programming/LIS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Created on 29-07-2019 16:21:24 by necronomicon
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <iomanip>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
using namespace std;

#define MP make_pair
#define PB push_back
#define ARR_MAX (int)1e5 //Max array length
#define INF (int)1e9 //10^9
#define EPS 1e-9 //10^-9
#define MOD 1000000007 //10^9+7
#define PI 3.1415926535897932384626433832795
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<Pii> VPii;
typedef vector<Vi> VVi;
typedef map<int,int> Mii;
typedef set<int> Si;
typedef multimap<int,int> MMii;
typedef multiset<int> MSi;
typedef unordered_map<int,int> UMii;
typedef unordered_set<int> USi;
typedef unordered_multimap<int,int> UMMii;
typedef unordered_multiset<int> UMSi;
typedef priority_queue<int> PQi;
typedef queue<int> Qi;
typedef deque<int> DQi;


int N;
Vi numbers;
Vi DP;

int bruteForce(int k){
Vi res;
for (int i = 0; i < k; i++)
{
if(numbers[i] < numbers[k]) res.push_back(bruteForce(i));
}
if(res.size() == 0) return 1;
return 1+(*max_element(res.begin(), res.end()));
}

int dpTopDown(int k){
if(k>=0 && DP[k]!=0) return DP[k];
Vi res;
for (int i = 0; i < k; i++)
{
if(numbers[i] < numbers[k]) res.push_back(dpTopDown(i));
}
if(res.size() == 0) return 1;
return DP[k] = 1+(*max_element(res.begin(), res.end()));
}

int dpBottomUp(){
for (int i = 0; i < N+1; i++)
{
int m = 0;
for (int j = 0; j < i; j++)
{
if(numbers[j] < numbers[i]) m = max(m, DP[j]);
}
m++;
DP[i] = m;
}
return (*DP.rbegin())-1;
}

int main (int argc, char const *argv[]) {
N = 9;
numbers = {-7, 10, 9, 2, 3, 8, 8, 1, 10};
numbers.push_back(INF); // adding extra element cuz otherwise we have to run func for every pos 0->N
cout << "Brute-Force: " << bruteForce(N)-1 << endl;

// Top Down
DP.clear();
DP = Vi(N+1, 0);
cout << "Top-Down: " << dpTopDown(N)-1 << endl;
for(int x: DP) cout << x << ' ';
cout << endl;

// Bottom Up
DP.clear();
DP = Vi(N+1, 0);
cout << "Bottom-Up: " << dpBottomUp() << endl;
for(int x: DP) cout << x << ' ';
cout << endl;

return EXIT_SUCCESS;
}
117 changes: 117 additions & 0 deletions Dynamic Programming/coinChange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Created on 29-07-2019 16:21:24 by necronomicon
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <iomanip>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
using namespace std;

#define MP make_pair
#define PB push_back
#define ARR_MAX (int)1e5 //Max array length
#define INF (int)1e9 //10^9
#define EPS 1e-9 //10^-9
#define MOD 1000000007 //10^9+7
#define PI 3.1415926535897932384626433832795
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<Pii> VPii;
typedef vector<Vi> VVi;
typedef map<int,int> Mii;
typedef set<int> Si;
typedef multimap<int,int> MMii;
typedef multiset<int> MSi;
typedef unordered_map<int,int> UMii;
typedef unordered_set<int> USi;
typedef unordered_multimap<int,int> UMMii;
typedef unordered_multiset<int> UMSi;
typedef priority_queue<int> PQi;
typedef queue<int> Qi;
typedef deque<int> DQi;


int N, targetSum;
Vi coinValue;
Vi DP;

int bruteForce(int s){
if(s == 0) return 0;
Vi res;
for(int coin: coinValue){
if(s >= coin) res.push_back(1+bruteForce(s-coin));
}
return *min_element(res.begin(), res.end());
}

int dpTopDown(int s){
if(s == 0) return 0;
if(DP[s] != -1) return DP[s];
Vi res;
for(int coin: coinValue){
if(s >= coin) res.push_back(1+dpTopDown(s-coin));
}
return DP[s] = *min_element(res.begin(), res.end());
}

int dpBottomUp(){
DP[0] = 0;
for (int i = 0; i <= targetSum; i++)
{
for(int coin: coinValue){
if(i >= coin){
DP[i] = 1 + min(DP[i], DP[i-coin]);
}
}
}
return DP[targetSum];
}

int main (int argc, char const *argv[]) {
N = 2;
targetSum = 10;
coinValue = {1, 5};

cout << "Brute-Force: " << bruteForce(targetSum) << endl;

// Top Down
DP.clear();
DP = Vi(targetSum+1, -1);
cout << "Top-Down: " << dpTopDown(targetSum) << endl;
for(int x: DP) cout << x << ' ';
cout << endl;

// Bottom Up
DP.clear();
DP = Vi(targetSum+1, INF);
cout << "Bottom-Up: " << dpBottomUp() << endl;
for(int x: DP) cout << x << ' ';
cout << endl;

return EXIT_SUCCESS;
}