Skip to content

Commit

Permalink
Added 874
Browse files Browse the repository at this point in the history
  • Loading branch information
MonitSharma authored Sep 4, 2024
1 parent 27477a2 commit 7beac22
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 874-walking-robot/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import List, Set, Tuple

class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
# Directions in the order of North, East, South, West
direction_vectors = [(0, 1), (1, 0), (0, -1), (-1, 0)]

# Initial position and direction (0 -> North, 1 -> East, 2 -> South, 3 -> West)
x, y = 0, 0
direction = 0

# Convert obstacles to a set of tuples for O(1) lookup
obstacle_set: Set[Tuple[int, int]] = set(map(tuple, obstacles))

# Variable to store the maximum distance squared
max_distance_squared = 0

for command in commands:
if command == -2:
# Turn left (counterclockwise)
direction = (direction - 1) % 4
elif command == -1:
# Turn right (clockwise)
direction = (direction + 1) % 4
else:
# Move forward command units
for _ in range(command):
# Calculate the next position
next_x = x + direction_vectors[direction][0]
next_y = y + direction_vectors[direction][1]

# Check if the next position is an obstacle
if (next_x, next_y) not in obstacle_set:
# Update the position
x, y = next_x, next_y
# Update the maximum distance squared
max_distance_squared = max(max_distance_squared, x * x + y * y)
else:
# Stop moving if an obstacle is encountered
break

return max_distance_squared

0 comments on commit 7beac22

Please sign in to comment.