Skip to content

Commit d1df3d7

Browse files
jeremymanninggithub-actions[bot]
authored andcommitted
Auto-solve daily LeetCode problem using GPT-5-mini
1 parent fb28932 commit d1df3d7

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

problems/2751/gpt5-mini.md

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
# [Problem 2751: Robot Collisions](https://leetcode.com/problems/robot-collisions/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4-
Robots move at the same speed; collisions only happen when a right-moving robot (R) is to the left of a left-moving robot (L). Sorting robots by position turns the problem into a left-to-right sweep where R robots can "wait" on a stack and L robots collide with the stacked R robots from the top. This resembles the classic asteroid-collision / parentheses-matching pattern. For each L we simulate collisions with the most recent R; health comparisons determine who dies or has its health decremented and whether collisions continue. We must return surviving robots' healths in the original input order.
4+
[Robots move simultaneously; collisions occur when a right-moving robot to the left meets a left-moving robot to the right. Positions are arbitrary (unsorted), so sort by position to simulate encounters left-to-right. A classic pattern: maintain a stack of right-moving robots (their index and current health). When we encounter a left-moving robot, it will potentially collide with robots on the stack (the right-movers encountered earlier). Simulate pairwise collisions between the current left-moving robot and the top of the stack until either the left robot dies or there are no more right-movers to collide with. Track survivors with their updated healths. Need to return survivors in the original input order.]
55

66
## Refining the problem, round 2 thoughts
7-
- Sort robots by position while keeping original indices, healths, and directions.
8-
- Use a stack to store right-moving robots (their original index and current health).
9-
- When we encounter a left-moving robot, simulate collisions against the stack top until the L dies or the stack is empty:
10-
- If R.health < L.health: R dies (pop), L.health -= 1, continue.
11-
- If R.health == L.health: both die (pop R, L becomes dead), stop.
12-
- If R.health > L.health: R.health -= 1 (update top), L dies, stop.
13-
- After processing all robots, any R robots left on the stack survive with their current health. Any L that survived (stack empty when processed) also survive with its remaining health.
14-
- Output survivors' healths in the input order (skip dead robots).
15-
- Time: O(n) amortized since each robot is pushed/popped at most once. Space: O(n).
16-
17-
Edge cases: unsorted positions (handled by sorting), ties in health (both removed), many consecutive collisions causing health decrements.
7+
[Sort robots by position ascending (so collisions happen between an earlier 'R' and a later 'L'). Use a stack to store active 'R' robots with mutable health. For each 'L' robot, while stack non-empty and current L is alive, compare healths:
8+
- If top_R.health < L.health: top_R dies (pop), L.health -= 1, continue (L may hit earlier R).
9+
- If top_R.health == L.health: both die (pop, L dies).
10+
- If top_R.health > L.health: L dies, top_R.health -= 1 (update top on stack).
11+
If an L survives all encounters, it's a survivor (record its health). After processing all robots, remaining R robots on the stack are survivors with their updated healths. Finally, output survivors in original order. Complexity: sorting O(n log n) + simulation O(n) amortized, space O(n). Edge cases: all R or all L (no collisions), robots with large health (decrement count bounded by n since each collision removes at least one robot).]
1812

1913
## Attempted solution(s)
2014
```python
@@ -23,45 +17,55 @@ from typing import List
2317
class Solution:
2418
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
2519
n = len(positions)
26-
# Build list of robots sorted by position
27-
robots = sorted(((positions[i], i, healths[i], directions[i]) for i in range(n)), key=lambda x: x[0])
28-
# stack holds tuples (original_index, current_health) for right-moving robots
20+
# Create list of robots sorted by position: (pos, original_index, health, direction)
21+
robots = sorted(
22+
[(positions[i], i, healths[i], directions[i]) for i in range(n)],
23+
key=lambda x: x[0]
24+
)
25+
26+
# Stack for right-moving robots: elements are [original_index, current_health]
2927
stack = []
30-
# result placeholder: -1 means dead; otherwise final health
31-
res = [-1] * n
28+
survivors = {} # map original_index -> health for surviving robots
3229

3330
for _, idx, h, d in robots:
3431
if d == 'R':
35-
stack.append((idx, h))
36-
else: # d == 'L'
32+
# Right-moving robot might collide with a future L, so push to stack
33+
stack.append([idx, h])
34+
else:
35+
# Left-moving robot will collide with right-moving robots on the stack
3736
cur_h = h
38-
# collide with right-moving robots on the stack
39-
while cur_h > 0 and stack:
40-
r_idx, r_h = stack[-1]
41-
if r_h < cur_h:
42-
# right robot dies, left robot loses 1 health and continues
37+
while stack and cur_h > 0:
38+
top_idx, top_h = stack[-1]
39+
if top_h < cur_h:
40+
# top R dies; L loses 1 health and continues
4341
stack.pop()
4442
cur_h -= 1
45-
elif r_h == cur_h:
43+
elif top_h == cur_h:
4644
# both die
4745
stack.pop()
4846
cur_h = 0
49-
else: # r_h > cur_h
50-
# left robot dies, right robot loses 1 health and stays
51-
stack[-1] = (r_idx, r_h - 1)
47+
break
48+
else:
49+
# top R survives but loses 1 health; L dies
50+
stack[-1][1] -= 1
5251
cur_h = 0
53-
# if left robot survived (no more R to collide), record its remaining health
52+
break
5453
if cur_h > 0:
55-
res[idx] = cur_h
54+
# L survived all collisions
55+
survivors[idx] = cur_h
5656

57-
# Any remaining right-moving robots in stack survive with their current health
58-
for r_idx, r_h in stack:
59-
res[r_idx] = r_h
57+
# Remaining right-moving robots on stack survive
58+
for idx, h in stack:
59+
survivors[idx] = h
6060

61-
# Return survivors' healths in original order (skip dead robots)
62-
return [res[i] for i in range(n) if res[i] != -1]
61+
# Collect survivors' healths in the original input order
62+
result = []
63+
for i in range(n):
64+
if i in survivors:
65+
result.append(survivors[i])
66+
return result
6367
```
6468
- Notes:
65-
- Approach: sort by position and simulate pairwise collisions using a stack for R robots; L robots collide with the nearest R on the left (stack top).
66-
- Complexity: O(n) time (each robot is pushed/popped at most once) and O(n) extra space for sorting + stack + result array.
67-
- Implementation details: we track current healths in the stack to account for reductions after collisions; original indices are preserved to produce output in input order.
69+
- We sort by position to process collisions in spatial order. The stack stores currently active right-moving robots; left-moving robots resolve collisions against the stack top(s).
70+
- Each robot is pushed/popped at most once, so the collision simulation is O(n) amortized. Total time complexity is O(n log n) due to initial sorting. Space complexity is O(n) for the stack and survivors map.
71+
- The survivors dictionary maps original indices to their final health so we can output in the order requested by the problem.

0 commit comments

Comments
 (0)