Push survivors
Updated
After the collision loop, push the newcomer iff it still exists. If it died, push nothing. If the stack emptied, the newcomer is the sole survivor so far. Same-direction items always push.
What to focus on
Do not push a destroyed newcomer. Do not skip pushing a survivor. The stack after the whole scan is the answer in order from bottom to top.
Worked examples
survives
a = [5], incoming 10 (same dir)- push 10
Result: [5, 10]
dies
a = [10], incoming -5- 10 wins
- do not push -5
Result: [10]
empty then push
a = [], incoming -5- nothing to fight
- push
Result: [-5]
Try it
Write pushSurvivor(stack, incoming). stack is the survivor stack. incoming is a non-zero integer. After resolving fights, push incoming only if it still lives. Return the stack. You may push onto empty. a = [5], incoming 10 (same dir) → [5, 10]. a = [10], incoming -5 → [10]. a = [], incoming -5 → [-5].
Incoming is a non-zero integer. You may push onto an empty survivor stack.