forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot-collisions.py
28 lines (27 loc) · 922 Bytes
/
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
# Time: O(nlogn)
# Space: O(n)
# sort, simulation, stack
class Solution(object):
def survivedRobotsHealths(self, positions, healths, directions):
"""
:type positions: List[int]
:type healths: List[int]
:type directions: str
:rtype: List[int]
"""
stk = []
for i in sorted(xrange(len(positions)), key=lambda x:positions[x]):
if directions[i] == 'R':
stk.append(i)
continue
while stk:
if healths[stk[-1]] == healths[i]:
healths[stk.pop()] = healths[i] = 0
break
if healths[stk[-1]] > healths[i]:
healths[i] = 0
healths[stk[-1]] -= 1
break
healths[stk.pop()] = 0
healths[i] -= 1
return [x for x in healths if x]