Skip to content

Commit

Permalink
Add problems from hackerrank
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonber committed Feb 17, 2017
1 parent 93bac56 commit 586507d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
40 changes: 40 additions & 0 deletions hackerrank/Algorithms/DynamicProgramming/TheCoinChangeProblem.cpp
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;
}

65 changes: 65 additions & 0 deletions hackerrank/Algorithms/Sorting/InsertionSort-Part1.cpp
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;
}

0 comments on commit 586507d

Please sign in to comment.