0% found this document useful (0 votes)
9 views6 pages

Find Combinations For Game Scoring - Coderust - Hacking The Coding Interview

The document discusses an algorithm to find the maximum sum of a subsequence in an array without including consecutive elements. It outlines the problem, provides hints, and presents a solution using dynamic programming with a linear runtime and memory complexity. Additionally, it includes code examples in various programming languages and suggests further optimizations for space usage.

Uploaded by

AshishManchanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Find Combinations For Game Scoring - Coderust - Hacking The Coding Interview

The document discusses an algorithm to find the maximum sum of a subsequence in an array without including consecutive elements. It outlines the problem, provides hints, and presents a solution using dynamic programming with a linear runtime and memory complexity. Additionally, it includes code examples in various programming languages and suggests further optimizations for space usage.

Uploaded by

AshishManchanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

02/11/2020 Find Combinations for Game Scoring - Coderust: Hacking the Coding Interview

Find Combinations for Game Scoring


Find the number of ways a player can score 'n' runs.

We'll cover the following

• Description
• Hints
• Try it yourself
• Solution
• Runtime complexity
• Memory complexity

Description #
Find an efficient algorithm to find the maximum sum of a subsequence in
an array so that no consecutive elements are part of this subsequence.

Consider the following examples:

The max sum of a subsequence with no consecutive elements in the


example below is 25:

1 6 10 14 -5 -1 2 -1 3

max sum = 25

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2J5P 1/6
02/11/2020 Find Combinations for Game Scoring - Coderust: Hacking the Coding Interview

The max sum of a subsequence with no consecutive elements in the


example below is 9:

1 -1 6 -4 2 2

max sum = 9

Note that we did not pick 10 and 14 at indices 2 and 3 because they are
adjacent. It is a requirement that we have to pick nonadjacent elements
only.

Hints #
Think along the lines of the Fibonacci sequence.

Try it yourself #
C++ Java Python JS Ruby

int find_max_sum_nonadjacent(vector<int>& a) {
// TODO: Write - Your - Code
return -1;
}

Solution #
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/j2J5P 2/6
02/11/2020 Find Combinations for Game Scoring - Coderust: Hacking the Coding Interview

A naive solution to this problem is to calculate the sum of all possible


subsequences with no adjacent elements and keep track of the
subsequence with the maximum sum. The time complexity of this
approach is O(n2 ).

This can be improved by using dynamic programming. Iterate over the


entire input array, and in every iteration pick the maximum of these two
values:

Max Sum of the last iteration.


Max Sum of second last iteration + current iteration index.

Let’s run through an example now:

Suppose our input array


is as shown. We will also
input 1 -1 6 -4 2 2
make a result array. In the
beginning, all elements of
the array are zero. result 0 0 0 0 0 0

Initial setup
1 of 7

C++ Java Python JS Ruby

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2J5P 3/6
02/11/2020 Find Combinations for Game Scoring - Coderust: Hacking the Coding Interview

int find_max_sum_nonadjacent(vector<int>& a) {

if (a.size() == 0) {
return 0;
}

if (a.size() == 1) {
return a[0];
}

std::vector<int> result(a.size(), 0);

result[0] = a[0];
for (int i = 1; i < a.size(); i++) {

result[i] = max(a[i], result[i-1]);

if ((i - 2) >= 0) {
result[i] = max(result[i], a[i] + result[i-2]);
}
}
return result[a.size() - 1];
}

int main() {
vector<int> v = {1, -1, 6, -4, 2, 2};
int sum = find_max_sum_nonadjacent(v);
cout << "Max sum of nonadjacent subsequence: " << sum;
}

This solution can be improved further by reducing space usage. We


don’t need to keep an array of previous maximum sums, only storing
the last two sums would suffice. Also, the algorithm can be modified
to track all the indices of this subarray with some additional data
structures.

0 1 2 3
Initial State
0 0 0 1

Results Array

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2J5P 4/6
02/11/2020 Find Combinations for Game Scoring - Coderust: Hacking the Coding Interview

1 of 6

C++ Java Python JS Ruby

// 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

MaxSum Subsequence - Nonadjacent … Coin Changing Problem

Mark as Completed

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2J5P 5/6
02/11/2020 Find Combinations for Game Scoring - Coderust: Hacking the Coding Interview

23% completed, meet the criteria and claim your course certi cate!
Buy Certificate

Ask a Question
Report
(https://discuss.educative.io/tag/ nd-combinations-for-game-scoring__dynamic-
an Issue
programming__coderust-hacking-the-coding-interview)

https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2J5P 6/6

You might also like