|
| 1 | +# Robot Collisions (Hard) |
| 2 | + |
| 3 | +**Problem ID:** 2751 |
| 4 | +**Date:** 2026-04-01 |
| 5 | +**Link:** https://leetcode.com/problems/robot-collisions/ |
| 6 | + |
| 7 | +## Approach |
| 8 | + |
| 9 | +To solve the "Robot Collisions" problem, we can utilize a stack-based approach to efficiently manage the interactions between robots as they move along a line. |
| 10 | + |
| 11 | +### Main Idea |
| 12 | +The key insight is to recognize that collisions only occur between robots moving towards each other: specifically, a robot moving right ('R') can collide with a robot moving left ('L'). Therefore, we can process the robots in the order of their positions, maintaining a stack to keep track of the surviving robots as we simulate their movements and collisions. |
| 13 | + |
| 14 | +### Approach |
| 15 | +1. **Sort Robots by Position**: First, we need to pair each robot's position, health, and direction, and then sort these pairs based on their positions. This allows us to process robots in the order they appear on the line. |
| 16 | + |
| 17 | +2. **Use a Stack for Collision Management**: We will use a stack to keep track of the robots that are still active: |
| 18 | + - If a robot is moving to the right ('R'), we simply push it onto the stack. |
| 19 | + - If a robot is moving to the left ('L'): |
| 20 | + - We check the top of the stack (the last robot that moved to the right): |
| 21 | + - If the stack is empty, the left-moving robot survives. |
| 22 | + - If the top robot is moving right, a collision occurs. We compare their healths: |
| 23 | + - If the left-moving robot has higher health, it survives, and the right-moving robot's health decreases by 1. |
| 24 | + - If the right-moving robot has higher health, it survives, and the left-moving robot is discarded. |
| 25 | + - If both have equal health, both are discarded. |
| 26 | + - Continue this process until no more collisions can occur or the stack is empty. |
| 27 | + |
| 28 | +3. **Final Result Extraction**: After processing all robots, the stack will contain the surviving robots in the order they were originally input. We can extract their health values and return them as the result. |
| 29 | + |
| 30 | +### Data Structures |
| 31 | +- **Stack**: To manage the active robots and handle collisions efficiently. |
| 32 | +- **Array/List**: To store the initial robot attributes (positions, healths, directions) and to generate the final output. |
| 33 | + |
| 34 | +### Complexity |
| 35 | +- **Time Complexity**: O(n log n) due to the initial sorting of the robots by position. The subsequent processing of the robots using the stack is O(n) in the worst case, leading to an overall complexity of O(n log n). |
| 36 | +- **Space Complexity**: O(n) for storing the stack and the sorted list of robots. |
| 37 | + |
| 38 | +This approach efficiently simulates the robot movements and resolves collisions while maintaining the order of surviving robots, ensuring that we meet the problem's requirements within the given constraints. |
0 commit comments