Skip to content

Instantly share code, notes, and snippets.

@tchomphoochan
Last active January 28, 2019 12:56
Show Gist options
  • Save tchomphoochan/4b5bf20242691f9c9acd94ee01515ec5 to your computer and use it in GitHub Desktop.
Save tchomphoochan/4b5bf20242691f9c9acd94ee01515ec5 to your computer and use it in GitHub Desktop.
int memo[MAX_N]; // initially set all to -1 to mark as "not done yet"
int change(int i) {
// base cases:
if (i == 0) return 0;
if (i < 0) return 1e9; // 1e9 = 1,000,000,000
// already solved cases:
if (memo[i] != -1)
return memo[i];
// recursive step:
int ans = 1 + min(change(i-1), min(change(i-3), change(i-4)));
memo[i] = ans; // memoize the answer
return ans;
}
// cout << change(6) << endl;
// result = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment