-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
hackerrank/Algorithms/DynamicProgramming/TheCoinChangeProblem.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,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<int> v; | ||
long long dp[55][255]; | ||
|
||
long long go (int i, int n) { | ||
if (n == 0) return 1; | ||
if (i == v.size() || n < 0) return 0; | ||
if (dp[i][n] != -1) return dp[i][n]; | ||
|
||
long long ans = 0; | ||
for (int j = i; j < v.size(); ++j) { | ||
long long a = 0; | ||
if (n - v[j] >= 0) { | ||
a = go(j, n - v[j]); | ||
} | ||
ans += a; | ||
} | ||
|
||
return dp[i][n] = ans; | ||
} | ||
|
||
int main() { | ||
int n, m; | ||
cin >> n >> m; | ||
|
||
for (int i = 0; i < m; ++i) { | ||
int c; | ||
cin >> c; | ||
v.push_back(c); | ||
} | ||
|
||
memset(dp, -1, sizeof dp); | ||
long long ans = go (0, n); | ||
cout << (n == 0 ? 0 : ans) << endl; | ||
|
||
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,65 @@ | ||
#include <map> | ||
#include <set> | ||
#include <list> | ||
#include <cmath> | ||
#include <ctime> | ||
#include <deque> | ||
#include <queue> | ||
#include <stack> | ||
#include <bitset> | ||
#include <cstdio> | ||
#include <vector> | ||
#include <cstdlib> | ||
#include <numeric> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <algorithm> | ||
using namespace std; | ||
/* Head ends here */ | ||
void print(vector<int> ar){ | ||
for(int i=0;i<ar.size();i++){ | ||
if(i>0) cout << " "; | ||
cout << ar[i]; | ||
} | ||
cout << endl; | ||
} | ||
|
||
void insertionSort(vector <int> ar) { | ||
int t = ar.size(),f = 0; | ||
int last = ar[t-1]; | ||
if(t==1) print(ar); | ||
else { | ||
for(int i=t-2; i>=0; i--){ | ||
if(last > ar[i]){ | ||
ar[i+1] = last; | ||
print(ar); | ||
break; | ||
} | ||
else { | ||
ar[i+1] = ar[i]; | ||
print(ar); | ||
} | ||
if(i==0 && !f) { | ||
ar[0] = last; | ||
print(ar); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* Tail starts here */ | ||
int main() { | ||
vector <int> _ar; | ||
int _ar_size; | ||
cin >> _ar_size; | ||
for(int _ar_i=0; _ar_i<_ar_size; _ar_i++) { | ||
int _ar_tmp; | ||
cin >> _ar_tmp; | ||
_ar.push_back(_ar_tmp); | ||
} | ||
|
||
insertionSort(_ar); | ||
|
||
return 0; | ||
} |