Stack · Medium
A deterministic simulator stores right-moving positive tokens and left-moving negative tokens in one ordered row. Only adjacent tokens moving toward each other interact. Zero is a fixed separator that survives and prevents interactions across it; it is not a moving token.
Return the stable surviving token sequence after all adjacent positive-then-negative collision boundaries are resolved.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums is the token row. A positive value moves right; a negative value moves left; its absolute value is its strength.
- A zero is an indestructible separator. Tokens on opposite sides of a zero never interact.
Output
Return value
- At a positive-then-negative boundary, remove the weaker token. Equal strengths remove both. Repeat newly exposed collisions until stable.
- Preserve survivor order. A negative token followed by a positive token moves apart and does not collide.
Constraints
- 0 <= nums.length <= 100000; each value is an integer between -1000000000 and 1000000000.
Examples
Example 1
{
"nums": [
5,
2,
-4
]
}Returns
[
5
]Example 2
{
"nums": [
5,
0,
-5
]
}Returns
[
5,
0,
-5
]Required complexity
O(n) time and O(n) auxiliary space