Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Coin Changing Problem - Coderust_ Hacking the Coding Interview

The Coin Changing Problem involves finding the number of ways to achieve a score using given scoring options (1, 2, and 4). Two solutions are presented: one using memoization with linear runtime and memory complexity, and another using dynamic programming with linear runtime and constant memory complexity. The document provides code examples and explanations for both approaches.

Uploaded by

AshishManchanda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Coin Changing Problem - Coderust_ Hacking the Coding Interview

The Coin Changing Problem involves finding the number of ways to achieve a score using given scoring options (1, 2, and 4). Two solutions are presented: one using memoization with linear runtime and memory complexity, and another using dynamic programming with linear runtime and constant memory complexity. The document provides code examples and explanations for both approaches.

Uploaded by

AshishManchanda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview

Coin Changing Problem


Given coin denominations and the total amount, find the number of ways to make the
change.

We'll cover the following

• 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

To score 5 runs, a player can score the following 10 ways:

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

// Scoring options are 1, 2, and 4


int scoring_options(int n) {
//TODO: Write - Your - Code
return -1;
}

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

Memoization is an optimization technique used to make programs faster


and improve their performance by storing the results of expensive
function calls and returning the cached result when the same input occurs
again. It saves the computed results for possible later reuse, rather than
recomputing them.

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:

S(n) = S(n − 1) + S(n − 2) + S(n − 4)

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

C++ Java Python JS Ruby

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 3/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview

int scoring_options_rec(int n, int result[]) {


if(n < 0){
return 0;
}

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;
}

// Initialize all elements to -1


std::vector<int> result(n + 1, -1);
result[0] = 1;

scoring_options_rec(n, &result[0]);

return result[n];
}

int main() {
cout << "Number of ways score 5 can be reached = " << scoring_options(5); //10
}

Solution 2: dynamic programming #


Runtime complexity #
The runtime complexity of this solution is linear, O(n).

Memory complexity #
The memory complexity of this solution is constant, O(1).

In this solution, we’ll use the dynamic programming approach to


building the solution bottom-up by storing the results in a fixed-size array.
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 4/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview

The maximum score is 4, so we need to save the last four ways to


calculate the number of ways for a given n.
On each iteration, the result will be the sum of values present at the
3rd , 2nd , and 0th index of the result array. This is because the result
at n is the sum of values at n − 1, n − 2 and n − 4.
We’ll slide the result towards the left and save the current result at
the last index.

Let’s have a look at this technique step by step for score = 5.

0 1 2 3
Initial State
0 0 0 1

Results Array
1 of 6

C++ Java Python JS Ruby

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2kkl 5/7
02/11/2020 Coin Changing Problem - Coderust: Hacking the Coding Interview

// Scoring options are 1, 2, and 4


int scoring_options(int n) {
if(n <= 0) {
return 0;
}

// Max score is 4. Hence we need to save


// last 4 ways to calculate the number of ways
// for a given n
std::vector<int> result(4, 0);

//save the base case on last index of the vector


result[3] = 1;

for(int i = 1; i <= n; i++) {


int sum = result[3] + result[2] + result[0];

//slide left all the results in each iteration


//result for current i will be saved at last index
result[0] = result[1];
result[1] = result[2];
result[2] = result[3];
result[3] = sum;
}

return result[3];
}

int main() {
cout << "Number of ways score 5 can be reached = " << scoring_options(5); //10
}

Back Next

Find Combinations for Game Scoring Levenshtein Distance

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

You might also like