Skip to content

Commit b36cba6

Browse files
authored
Update README.md
1 parent 7f8adac commit b36cba6

File tree

1 file changed

+24
-1
lines changed
  • solution/2700-2799/2751.Robot Collisions

1 file changed

+24
-1
lines changed

solution/2700-2799/2751.Robot Collisions/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,30 @@ tags:
9090
#### Python3
9191

9292
```python
93-
93+
class Solution:
94+
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
95+
temp = {x: i for i,x in enumerate(positions)}
96+
97+
stack = []
98+
for x in sorted(positions):
99+
i = temp[x]
100+
101+
if directions[i] == "R":
102+
stack.append(i)
103+
else:
104+
while stack and healths[i]:
105+
j = stack.pop()
106+
if healths[i] > healths[j]:
107+
healths[j] = 0
108+
healths[i] -= 1
109+
elif healths[i] < healths[j]:
110+
healths[i] = 0
111+
healths[j] -= 1
112+
stack.append(j)
113+
else:
114+
healths[i] = healths[j] = 0
115+
116+
return [item for item in healths if item > 0]
94117
```
95118

96119
#### Java

0 commit comments

Comments
 (0)