Crushing Heavy Boxes Solution
Problem Statement
You are managing a warehouse conveyor belt where boxes are stacked sequentially. Each box is represented by an array of two integers [weight, color]. You process the boxes from left to right, placing them onto a single vertical stack. When a new box is placed on the stack, it may crush the box directly below it if both of the following conditions are met: 1. They have the same color. 2. The incoming box has a strictly greater weight than the box on the top of the stack. If a box is crushed, it is permanently removed from the stack, and the incoming box is then compared with the new top of the stack. This crushing process cascades until the incoming box cannot crush the top box (either because the stack is empty, the top box has a different color, or the top box has a weight greater than or equal to the incoming box). At that point, the incoming box is placed on top of the stack. Return an array of the weights of the boxes remaining in the stack after processing all input boxes in the given order.
Examples
Constraints
- 1 <= boxes.length <= 10^5
- boxes[i].length == 2
- 1 <= boxes[i][0], boxes[i][1] <= 10^9
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive Workspace