From 7d9ea39a979f36278a180a535571b0e7abe59472 Mon Sep 17 00:00:00 2001 From: Yash <75321458+Yashwanth137@users.noreply.github.com> Date: Sat, 13 Jul 2024 21:50:07 +0530 Subject: [PATCH] Create Solution.py --- .../2751.Robot Collisions/Solution.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.py diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.py b/solution/2700-2799/2751.Robot Collisions/Solution.py new file mode 100644 index 0000000000000..6415e99fc3727 --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.py @@ -0,0 +1,24 @@ +class Solution: + def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]: + temp = {x: i for i,x in enumerate(positions)} + + stack = [] + for x in sorted(positions): + i = temp[x] + + if directions[i] == "R": + stack.append(i) + else: + while stack and healths[i]: + j = stack.pop() + if healths[i] > healths[j]: + healths[j] = 0 + healths[i] -= 1 + elif healths[i] < healths[j]: + healths[i] = 0 + healths[j] -= 1 + stack.append(j) + else: + healths[i] = healths[j] = 0 + + return [item for item in healths if item > 0]