-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRobot_Collisions.py
34 lines (27 loc) · 1.11 KB
/
Robot_Collisions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
n = len(positions)
pairs = list(zip(positions, healths, list(directions), list(range(n))))
pairs.sort(key=lambda x:x[0])
stack = []
for i in range(n):
current = pairs[i]
if not stack:
stack.append(current)
continue
if current[2] == 'R':
stack.append(current)
continue
while stack and stack[-1][2] == 'R' and current[2] == 'L':
tmp = stack.pop()
if tmp[1] < current[1]:
current = (current[0], current[1] - 1, current[2], current[3])
elif tmp[1] > current[1]:
current = (tmp[0], tmp[1] - 1, tmp[2], tmp[3])
else:
current = None
break
if current:
stack.append(current)
stack.sort(key=lambda x:x[3])
return [x[1] for x in stack]