ORIGINAL CODING QUESTION
Collapse a bonus chain
Positive bonus tokens stand in a row. Removing a token earns left × value × right, where left and right are the values of its nearest tokens still standing on each side, and a missing side counts as 1. Remove every token in any order you like and return the largest total earnings.
Original exercise · hard. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n token values.
Constraints
1 ≤ n ≤ 8; 1 ≤ value ≤ 9.
Example 1
Input
3 3 1 5
Output
35
Remove 1 (3×1×5), then 3 (1×3×5), then 5 (5): 15 + 15 + 5 = 35.
Example 2
Input
1 7
Output
7
The lone token earns 1×7×1.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Choosing the first token to remove makes the subproblems depend on each other.
Hint 2
Instead pick the LAST token removed inside a range; its neighbours are then the range boundaries.
Hint 3
Pad the row with 1 at both ends and fill best[l][r] for open intervals by increasing length.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →