Skip to content

Commit fdb6fbc

Browse files
chore: add LeetCode daily solution
1 parent 1a625ea commit fdb6fbc

5 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] robotCollisions(int[] positions, int[] healths, String directions) {
5+
int n = positions.length;
6+
List<int[]> robots = new ArrayList<>();
7+
8+
for (int i = 0; i < n; i++) {
9+
robots.add(new int[]{positions[i], healths[i], directions.charAt(i)});
10+
}
11+
12+
// Sort robots based on their positions
13+
robots.sort(Comparator.comparingInt(a -> a[0]));
14+
15+
Stack<int[]> stack = new Stack<>();
16+
17+
for (int[] robot : robots) {
18+
int pos = robot[0];
19+
int health = robot[1];
20+
char dir = robot[2];
21+
22+
if (dir == 'R') {
23+
stack.push(new int[]{pos, health});
24+
} else {
25+
while (!stack.isEmpty() && stack.peek()[1] > 0) {
26+
int[] lastRobot = stack.peek();
27+
if (lastRobot[1] > health) {
28+
health--;
29+
} else if (lastRobot[1] < health) {
30+
stack.pop();
31+
health--;
32+
} else {
33+
stack.pop();
34+
health--;
35+
break;
36+
}
37+
}
38+
if (health > 0) {
39+
stack.push(new int[]{pos, health});
40+
}
41+
}
42+
}
43+
44+
int[] result = new int[stack.size()];
45+
for (int i = stack.size() - 1; i >= 0; i--) {
46+
result[i] = stack.pop()[1];
47+
}
48+
49+
return result;
50+
}
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var surviveRobots = function(positions, healths, directions) {
2+
const robots = positions.map((pos, i) => ({ pos, health: healths[i], dir: directions[i] }));
3+
robots.sort((a, b) => a.pos - b.pos);
4+
5+
const stack = [];
6+
7+
for (const robot of robots) {
8+
while (stack.length && robot.dir === 'L' && stack[stack.length - 1].dir === 'R') {
9+
const top = stack.pop();
10+
if (top.health > robot.health) {
11+
robot.health--;
12+
} else if (top.health < robot.health) {
13+
continue;
14+
}
15+
}
16+
if (robot.health > 0) {
17+
stack.push(robot);
18+
}
19+
}
20+
21+
return stack.map(robot => robot.health);
22+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def surviveRobots(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
3+
robots = sorted(zip(positions, healths, directions))
4+
stack = []
5+
6+
for pos, health, direction in robots:
7+
if direction == 'R':
8+
stack.append([health])
9+
else:
10+
while stack and stack[-1][0] > 0:
11+
if stack[-1][0] < health:
12+
health -= 1
13+
stack.pop()
14+
elif stack[-1][0] == health:
15+
health = 0
16+
stack.pop()
17+
break
18+
else:
19+
health = 0
20+
break
21+
if health > 0:
22+
stack.append([health])
23+
24+
return [h[0] for h in stack if h[0] > 0]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
410410
- 2026-03-29 — [Check if Strings Can be Made Equal With Operations I](https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i/) (Easy) → `Easy/2026-03-29-2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I`
411411
- 2026-03-30 — [Check if Strings Can be Made Equal With Operations II](https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-ii/) (Medium) → `Medium/2026-03-30-2840-Check-if-Strings-Can-be-Made-Equal-With-Operations-II`
412412
- 2026-03-31 — [Lexicographically Smallest Generated String](https://leetcode.com/problems/lexicographically-smallest-generated-string/) (Hard) → `Hard/2026-03-31-3474-Lexicographically-Smallest-Generated-String`
413+
- 2026-04-01 — [Robot Collisions](https://leetcode.com/problems/robot-collisions/) (Hard) → `Hard/2026-04-01-2751-Robot-Collisions`

0 commit comments

Comments
 (0)