VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximizing-merit-points-in-training-program/

⇱ Maximizing Merit Points in Training Program - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximizing Merit Points in Training Program

Last Updated : 23 Jul, 2025

Geek is going for a training program. He can perform any of these activities: Running, Fighting, and Learning Practice. Each activity has some point on each day. As Geek wants to improve all his skills, he can't do the same activity on two consecutive days. Help Geek to maximize his merit points as we are given a 2D array of points mat[][], corresponding to each day and activity.

Examples:

Input: mat[][] = [[1, 2, 5],
[3, 1, 1],
[3, 3, 3]]
Output: 11
Explanation: Geek will learn a new move and earn 5 point then on second day he will do running and earn 3 point and on third day he will do fighting and earn 3 points so, maximum point is 11.

Input: mat[][] = [[1, 10, 5],
[7, 0, 3],
[5, 10, 0],
[10, 2, 4]]
Output: 37
Explanation: Geek will fight and earn 10 points. On the second day, he will run and earn 7 points. On the third day, he will fight again and earn 10 points, and on the fourth day, he will run, earning another 10 points. Therefore, the maximum points are 10 + 7 + 10 + 10 = 37 points

Using Recursion - O(2^n) Time and O(n) Space

We can identify a recursive pattern in this problem. There are two state variables:

  • i: Current row index.
  • prev: Column index of the action chosen in the previous row (initialized to -1).

Recursive Steps:

  • For each column j (0 to 2), if j != prev, calculate:
    • maxi = max(maxi, mat[i][j] + calculateMaximumPoints(i+1, j, mat))
  • Base Case: If i == n, return 0 (no more rows to process).

Recurrence Relation:

  • calculateMaximumPoints(i, prev, mat) = max(mat[i][j] + calculateMaximumPoints(i+1, j, mat)) where j != prev

Base Case: If i == n (all elements have been processed), return 0


Output
11

Using Top-Down Dp (Memoization) - O(n) Time and O(n) Space

The recursive solution can be optimized using Dynamic Programming (DP) due to the following properties:

Optimal Substructure: The solution for a state, calculateMaximumPoints(i, prev), depends on the results of subproblems:

  • calculateMaximumPoints(i + 1, j) for valid actions j (j != prev).

Overlapping Subproblems: Many states, such as calculateMaximumPoints(2, 0), are recomputed multiple times in recursion from calculateMaximumPoints(1, 1) and calculateMaximumPoints(1, 2) . This can be avoided by memoization.

  • There are two parameter that change in the recursive solution: i going from 0 to n-1prev going from 0 to 2. So we create a 2D array memo[][] of size n*3 for memoization.
  • We initialize this array as -1 to indicate nothing is computed initially.
  • Now we modify our recursive solution to first check if the value is -1, then only make recursive calls. This way, we avoid re-computations of the same subproblems.

Output
11

Using Bottom-Up Dp (Tabulation) - O(n) Time and O(n) Space

We create a 2D array dp[n+1][3], where dp[i][j] represents the maximum points achievable from row i with action j.

We fill the dp array as follows:

  • We initialize all values of dp[i][j] to 0 because initially, there are no points.
  • Iterate over the rows of mat[][] from bottom to top (i.e., from row n-1 to row 0).
  • For each row i, iterate over all possible actions j (i.e., 0, 1, and 2).

For each action j:

  • We check all possible actions k in the next row i+1.
  • If k is not equal to j (i.e., avoid repeating the same action), we update dp[i][j] as follows: dp[i][j] = max(dp[i][j], dp[i+1][k])
  • After considering all possible actions for row i, we add the points from the current row: dp[i][j] += mat[i][j]

This can be explained as there are two cases: either we take the element mat[i][j] or we don’t. We take an element only when it’s allowed, meaning we can’t repeat the same action (j). The value in dp[i][j] is calculated by considering the maximum points we can get from the next row i+1 with a different action k, then adding the current row's points mat[i][j]. The final answer is the maximum value from dp[0][0], dp[0][1], and dp[0][2].


Output
11
Comment