Coin Changing Problem - Coderust_ Hacking the Coding Interview
Coin Changing Problem - Coderust_ Hacking the Coding Interview
• Description
• Hints
• Try it yourself
• Solution 1: memoization
• Runtime complexity
• Memory complexity
• Solution 2: dynamic programming
• Runtime complexity
• Memory complexity
Description #
Imagine a game where a player can score 1, 2, or 4 runs. Given a score, n,
find the total number of ways to score n runs.
For example, to score 3 runs, a player can score in the following 3 ways:
1 1 1
n=3 1 2
2 1
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 1/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview
1 1 1 1 1
1 1 1 2
1 1 2 1
1 2 1 1
1 2 2
n=5
1 4
2 1 1 1
2 1 2
2 2 1
4 1
Hints #
Use Fibonacci numbers.
Use the memoization technique.
Use principles of dynamic programming.
Try it yourself #
C++ Java Python JS Ruby
Solution 1: memoization #
Runtime complexity #
The runtime complexity of this solution is linear, O(n).
Memory complexity #
The memory complexity of this solution is linear, O(n).
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 2/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview
The scoring options are 1, 2, and 4. To find the number of ways a player
can score n runs, the recurrence relation is as follows:
We’ll build the solution top-down and store the found results in a result
array. This is classic memoization. Here is the visual representation of
recursion with memoization. The green nodes. represent recursive calls
where stored results are fetched from the results array.
0 1 2 3 4 5
result
1 -1 -1 -1 -1 -1
score_opt(5)
1 of 14
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 3/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview
if(result[n] > 0) {
return result[n];
}
// Memoize
result[n] = scoring_options_rec(n - 1, result) +
scoring_options_rec(n - 2, result) +
scoring_options_rec(n - 4, result);
return result[n];
}
int scoring_options(int n) {
if(n <= 0) {
return 0;
}
scoring_options_rec(n, &result[0]);
return result[n];
}
int main() {
cout << "Number of ways score 5 can be reached = " << scoring_options(5); //10
}
Memory complexity #
The memory complexity of this solution is constant, O(1).
0 1 2 3
Initial State
0 0 0 1
Results Array
1 of 6
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 5/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview
return result[3];
}
int main() {
cout << "Number of ways score 5 can be reached = " << scoring_options(5); //10
}
Back Next
Mark as Completed
23% completed, meet the criteria and claim your course certi cate!
Buy Certificate
Ask a Question
Report an
(https://discuss.educative.io/tag/coin-changing-problem__dynamic-
Issue
programming__coderust-hacking-the-coding-interview)
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 6/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 7/7