![]() |
VOOZH | about |
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
Table of Content
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
ito remove last in a given range[left, right]. The skill from removingilast 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))
182
Time complexity: O(4n/(n3/2)), time complexity is exponential due to the recursive partitioning.
Auxiliary Space: O(n), recursive stack.
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
- 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].
- 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.
182
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 indexiandj, 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
182