Count Robot Groups Problem
Description
LeetCode Problem 4045.
You are given a strictly increasing integer array position, where position[i] is the initial position of the i^th robot at time t = 0.
You are also given an integer array speed, where speed[i] is the constant speed of the i^th robot in units per second, and an integer distance.
Time is continuous and measured in seconds. A robot or group with speed v moves v * t units to the right over any interval of t seconds.
Whenever the distance between two robots or groups becomes at most distance, they merge into a single group.
If multiple robots or groups satisfy the merging condition at the same time, all merges happen simultaneously. In particular, every connected collection of robots or groups whose consecutive positions differ by at most distance merges into one group.
After a merge, the resulting group takes the current position and speed of the rightmost robot in that group. Once merged, robots never separate.
Return the number of groups remaining after all possible merges have occurred.
Example 1:
Input: position = [1,5,6,20], speed = [4,3,2,3], distance = 1
Output: 2
Explanation:

- Initially, the groups are {R_1}, {R_2}, {R_3}, and {R_4}.
- At t = 0, the robots R_2 and R_3 at positions 5 and 6, respectively, merge because they are 1 unit apart. The resulting group moves with the position and speed of the rightmost robot R_3. The groups are now {R_1}, {R_2, R_3}, and {R_4}.
- Later at t = 2, the robot R_1 catches up to the group {R_2, R_3} and merges with it. The groups are now {R_1, R_2, R_3} and {R_4}.
Thus, the answer is 2.
Example 2:
Input: position = [1,5,9], speed = [3,2,2], distance = 2
Output: 2
Explanation:

- Initially, the groups are {R_1}, {R_2}, and {R_3}.
- At t = 2, the robot R_1 catches up to the robot R_2 and merges with it. The resulting group moves with the position and speed of the rightmost robot R_2. The groups are now {R_1, R_2} and {R_3}.
Thus, the answer is 2.
Example 3:
Input: position = [9], speed = [8], distance = 5
Output: 1
Explanation:
Initially, there is only one group. Therefore, the answer is 1.
Constraints:
1 <= position.length == speed.length <= 10^51 <= position[i], speed[i], distance <= 10^9positionis strictly increasing.
Solution
Approach: Monotonic Stack of Absorption Times
A merged group moves exactly like its rightmost robot, so at any moment a group is just that robot's straight line position[j] + speed[j] * t. Robot i is absorbed when it gets within distance of the group on its right, and the answer is the number of robots that are never absorbed.
Handle robots from right to left. Let T[i] be the time robot i is absorbed (infinite if never). While robot i looks to the right, the group next to it is led by robot j as long as j has not been absorbed yet. So the leaders it sees are the robots j > i whose T[j] is larger than every T between i and j. Their times strictly increase, and they split time into windows: leader j is the neighbour during [T[previous leader], T[j]]. Keep these leaders on a stack, with the smallest T on top.
For robot i, walk down the stack window by window and find the earliest time in the window when position[j] + speed[j] * t - position[i] - speed[i] * t <= distance. The gap is linear in t, so this is either the start of the window or the point where it reaches distance (only possible when speed[i] > speed[j], and only if that point lies before the window ends). Times are fractions with numerator and denominator up to 10^9, so cross-multiplication fits in 64 bits.
- If no window works,
iis never absorbed: it is a final group, and it becomes the only leader (clear the stack). - Otherwise
T[i]is the time found. Every leader passed on the way has a smaller time, so it is popped, andiis pushed. The leader where we stopped is popped too if its time is not larger thanT[i].
Every leader is popped at most once, so the scan is amortized.
Complexity: O(n) time and O(n) space.
class Solution:
def countGroups(self, position: List[int], speed: List[int], distance: int) -> int:
n = len(position)
# Each group moves like its rightmost robot. t[i] = time robot i is absorbed by the group on its right
# (a fraction num / den, den == 0 means never). The stack keeps the robots whose absorption times
# strictly increase from the top down: they are, in turn, the rightmost robot of the group to the right.
stack = [(n - 1, 1, 0)]
ans = 1
for i in range(n - 2, -1, -1):
an, ad = 0, 1 # start of the current time window
found = None
k = len(stack) - 1
while k >= 0:
j, tn, td = stack[k]
gap = position[j] - position[i] - distance
dv = speed[i] - speed[j]
if gap * ad - dv * an <= 0: # already close enough when the window starts
found = (an, ad)
break
if dv > 0 and (td == 0 or gap * td <= tn * dv): # i catches up before the window ends
found = (gap, dv)
break
an, ad = tn, td
k -= 1
if found is None:
stack = [(i, 1, 0)]
ans += 1
else:
del stack[k + 1:]
tn, td = stack[k][1], stack[k][2]
if td != 0 and tn * found[1] <= found[0] * td:
stack.pop()
stack.append((i, found[0], found[1]))
return ans