Maximize the Score from Removing Equal-Color Runs

Maximize the Score from Removing Equal-Color Runs

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Given the sequence boxes, repeatedly remove a contiguous nonempty run of k boxes of one color and earn k*k points. Remaining boxes close the gap. Return the maximum total score after removing every box. ```cpp class Solution { public: int removeBoxes(vector<int>& boxes); }; ``` ### Examples **Example 1:** ```text boxes = [2,1,2] Output: 5 Explanation: Remove the middle box for 1, then the two 2s for 4. ``` **Example 2:** ```text boxes = [4,4,3,3] Output: 8 Explanation: Removing each two-box run gives 4+4. ``` ### Constraints - Box count: 1..100. Each color is encoded by an integer in 1..100. ### Clarifying Questions Keep the score quadratic in removed-run length and retain state for same-colored boxes that can merge later. ```hint Read the boundary condition What extra information must an interval state remember about boxes saved beside its right endpoint? ``` ### Contract considerations Keep the score quadratic in removed-run length and retain state for same-colored boxes that can merge later.
Google logo
Google
Jul 7, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given the sequence boxes, repeatedly remove a contiguous nonempty run of k boxes of one color and earn k*k points. Remaining boxes close the gap. Return the maximum total score after removing every box.

class Solution {
public:
    int removeBoxes(vector<int>& boxes);
};

Examples

Example 1:

boxes = [2,1,2]
Output: 5
Explanation: Remove the middle box for 1, then the two 2s for 4.

Example 2:

boxes = [4,4,3,3]
Output: 8
Explanation: Removing each two-box run gives 4+4.

Constraints

  • Box count: 1..100. Each color is encoded by an integer in 1..100.

Clarifying Questions Guidance

Keep the score quadratic in removed-run length and retain state for same-colored boxes that can merge later.

Contract considerations

Keep the score quadratic in removed-run length and retain state for same-colored boxes that can merge later.