VOOZH about

URL: https://www.geeksforgeeks.org/dsa/burst-balloon-to-maximize-coins/

⇱ MaxSkill: Optimally Removing People to Maximise Skill - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

MaxSkill: Optimally Removing People to Maximise Skill

Last Updated : 11 Jul, 2025

Given a queue of n people, indexed from 0 to n - 1. Each person has a rating represented by an array arr[], where arr[i] represents the rating of the i-th person. The skill of a person depends not only on their rating but also on their immediate neighbours.

If you remove the i-th person from the queue, you gain a skill value of arr[i - 1] * arr[i] * arr[i + 1]. If i - 1 or i + 1 is out of bounds, assume there is an implicit person with a rating of 1 at that position.

Your task is to remove all n people one by one in an optimal order to maximize the total skill value. After removing a person, the remaining queue should be rearranged in their original order before the next removal.

Return the maximum total skill you can obtain by removing the people optimally.

Examples :

Input: arr[] = [5, 10]
Output: 60
Explanation:

  • Remove person with rating 5 → Skill gained = 1 * 5 * 10 = 50, remaining queue: [10].
  • Remove person with rating 10 → Skill gained = 1 * 10 * 1 = 10, total skill = 50 + 10 = 60.

Input: arr[] = [3, 2, 5, 8]
Output: 182
Explanation:

  • Remove person with rating 2 → Skill gained = 3 * 2 * 5 = 30, remaining queue: [3, 5, 8].
  • Remove person with rating 5 → Skill gained = 3 * 5 * 8 = 120, remaining queue: [3, 8].
  • Remove person with rating 3 → Skill gained = 1 * 3 * 8 = 24, remaining queue: [8].
  • Remove person with rating 8 → Skill gained = 1 * 8 * 1 = 8, total skill = 30 + 120 + 24 + 8 = 182

[Naive Approach] Using Recursion

To solve the problem using recursion, let's consider a recursive approach by focusing on each person removal individually and calculating the maximum skill at each step.

To maximize the total skill value, we can choose a person i to remove last in a given range [left, right]. The skill from removing i last is given by: arr[left-1]*arr[i]*arr[right+1] plus the maximum skill from the left subarray[left, i-1] and right subarray[i+1, right].

This gives the recurrence relation:

  • maxSkill(l, r) = max(arr[l-1] * arr[i] * arr[r+1] + maxSkill(l, i-1) + maxSkill(i+1, r))

Output
182

Time complexity: O(4n/(n3/2)), time complexity is exponential due to the recursive partitioning.
Auxiliary Space: O(n), recursive stack.

[Better Approach] Using Top-Down DP (Memoization) - O(n^3) Time and O(n^2) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:

  1. Optimal Substructure: TThe maximum skill value obtained from removing people in a range [left, right] depends on the optimal solution of subproblems [left, k-1] and [k+1, right], where k is the last person removed in the current range. By selecting each person k as the last to remove in the current range, we can combine the results of these subproblems to determine the maximum skill achievable for the range [left, right].
  2. Overlapping Subproblems: In a purely recursive approach, the same subproblems are calculated multiple times. For example, when calculating the maximum skill for a range [1, n], the recursive calls may repeatedly calculate the maximum skill for the same subranges, such as [1, k-1] or [k+1, n]. This repetition leads to overlapping subproblems.

To avoid recomputing results for the same subproblems, we use a 2D DP arraydp of size (n+2) × (n+2) (as virtual persons with a rating of 1 are also added at both ends of the arr[] to simplify the handling of boundary conditions), where dp[left][right] stores the maximum skill value obtainable from removing all people in the range [left, right].

We initialize all entries in dp to -1 and populate them using memoization as we calculate optimal subproblems.


Output
182

[Expected Approach] Using Bottom-Up DP (Tabulation) - O(n^3) Time and O(n^2) Space

The approach is similar to the previous one. However, instead of breaking down the problem recursively, we iteratively build up the solution by calculating it in a bottom-up manner.

We maintain a dp[][] table such that dp[i][j] stores the maximum skill value that can be obtained by removing all the people between index i and j, inclusive.

This eliminates the need for recursion and avoids redundant computations, making the solution more efficient. The bottom-up dynamic programming (DP) approach ensures that smaller subproblems are solved first, and their results are used to construct solutions for larger subarrays


Output
182
Comment
Article Tags:
Article Tags: