Skip to content

Commit 7d9ea39

Browse files
authored
Create Solution.py
1 parent 7f8adac commit 7d9ea39

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
3+
temp = {x: i for i,x in enumerate(positions)}
4+
5+
stack = []
6+
for x in sorted(positions):
7+
i = temp[x]
8+
9+
if directions[i] == "R":
10+
stack.append(i)
11+
else:
12+
while stack and healths[i]:
13+
j = stack.pop()
14+
if healths[i] > healths[j]:
15+
healths[j] = 0
16+
healths[i] -= 1
17+
elif healths[i] < healths[j]:
18+
healths[i] = 0
19+
healths[j] -= 1
20+
stack.append(j)
21+
else:
22+
healths[i] = healths[j] = 0
23+
24+
return [item for item in healths if item > 0]

0 commit comments

Comments
 (0)