Robot Collisions - Complete Solution Guide
Robot Collisions is LeetCode problem 2751, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There are n 1-indexed robots, each having a position on a line, health, and movement direction. You are given 0-indexed integer arrays positions , healths , and a string directions ( directions[i] is either 'L' for left or 'R' for right ). All integers in positions are unique . All robots start moving on the line simultaneously at the same speed in their given directions. If two robots ever share the same position while moving, they will collide . If two robots collide, the robot with lower heal
Detailed Explanation
The problem describes a scenario where robots with unique positions, health points, and movement directions ('L' for left, 'R' for right) move simultaneously on a line. When two robots collide (occupy the same position), their health points are compared. The robot with lower health is removed, and the other robot's health decreases by one. If they have equal health, both are removed. The goal is to return the health values of the surviving robots in their original order from the input.
Solution Approach
The provided solution first sorts the robots based on their initial positions to simulate the collisions correctly. Then, it uses a stack to keep track of robots moving to the right. When a robot moving to the left encounters a right-moving robot (top of the stack), they collide. The collision is resolved by comparing their health points, and the surviving robot (if any) is updated or re-pushed onto the stack if it's moving right or continues colliding to the left if it is moving left. After processing all robots, the solution filters the healths array to return only the positive health values of the surviving robots in their original order.
Step-by-Step Algorithm
- Step 1: Create an array of indices and sort it based on the robots' positions.
- Step 2: Initialize an empty stack to store the indices of robots moving to the right ('R').
- Step 3: Iterate through the sorted indices:
- Step 4: If the robot at the current index is moving to the right, push its index onto the stack.
- Step 5: If the robot is moving to the left, enter a `while` loop as long as the stack is not empty and the robot's health is greater than 0. Pop the index of the right-moving robot from the stack.
- Step 6: Compare the health of the left-moving robot and the right-moving robot. Update the health of both robots based on the collision rules (remove the weaker one or both if equal health). If the right-moving robot survived, push its index back onto the stack.
- Step 7: After processing all robots, create a result list and add the remaining positive health values from the original healths array.
- Step 8: Return the list of surviving robot healths.
Key Insights
- Insight 1: Sorting by position is crucial to process collisions in the correct order as they happen on the number line.
- Insight 2: A stack is an effective data structure for keeping track of robots moving right ('R') and waiting for potential collisions with left-moving robots ('L').
- Insight 3: In-place modification of the healths array avoids the need for extra space to track robot health.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Stack, Sorting, Simulation.
Companies
Asked at: Deutsche Bank, Samsung, Sprinklr.