Advertisement

Walking Robot Simulation - LeetCode 874 Solution

Walking Robot Simulation - Complete Solution Guide

Walking Robot Simulation is LeetCode problem 874, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A robot on an infinite XY-plane starts at point (0, 0) facing north. The robot receives an array of integers commands , which represents a sequence of moves that it needs to execute. There are only three possible types of instructions the robot can receive: -2 : Turn left 90 degrees. -1 : Turn right 90 degrees. 1 <= k <= 9 : Move forward k units, one unit at a time. Some of the grid squares are obstacles . The i th obstacle is at grid point obstacles[i] = (x i , y i ) . If the robot runs into an

Detailed Explanation

The problem asks us to simulate a robot moving on an infinite XY-plane from the origin (0, 0). The robot starts facing north and receives a sequence of commands. Commands can be turning left (-2), turning right (-1), or moving forward a specific number of units (1 to 9). The robot needs to avoid obstacles, which are also given as input. If the robot encounters an obstacle while moving, it stops at the location immediately before the obstacle. The goal is to find the maximum squared Euclidean distance the robot reaches from the origin during its path.

Solution Approach

The solution involves simulating the robot's movement step by step according to the commands. We keep track of the robot's current position (x, y) and its current direction. We use an array to store the (dx, dy) changes for each direction (North, East, South, West). A hash set stores the coordinates of the obstacles for fast lookup. For each command, we update the robot's direction or move it forward one unit at a time until the commanded distance is reached or an obstacle is encountered. After each command (or move if the command moves the robot), we update the maximum squared Euclidean distance from the origin and return it.

Step-by-Step Algorithm

  1. Step 1: Initialize robot's position (x, y) to (0, 0), direction index (dir_idx) to 0 (North), and maximum squared distance (max_dist_sq) to 0.
  2. Step 2: Create a directions array or list to store the (dx, dy) changes for each direction: North (0, 1), East (1, 0), South (0, -1), West (-1, 0).
  3. Step 3: Create a hash set (or similar data structure) to store the obstacle coordinates for fast lookup.
  4. Step 4: Iterate through the commands array:
  5. Step 5: If the command is -1 (turn right), increment the direction index (dir_idx = (dir_idx + 1) % 4).
  6. Step 6: If the command is -2 (turn left), decrement the direction index (dir_idx = (dir_idx + 3) % 4). We use addition by 3 and modulo to ensure a positive result.
  7. Step 7: If the command is a positive integer (move forward), get the (dx, dy) changes for the current direction from the directions array.
  8. Step 8: Move the robot forward one unit at a time until the commanded distance is reached or an obstacle is encountered. For each unit moved, check if the next coordinate (x + dx, y + dy) is in the obstacle set. If it is, break the inner loop.
  9. Step 9: Update the robot's position (x += dx, y += dy).
  10. Step 10: Update the maximum squared distance (max_dist_sq = max(max_dist_sq, x*x + y*y)).
  11. Step 11: After iterating through all the commands, return the maximum squared distance (max_dist_sq).

Key Insights

  • Insight 1: Representing directions: Using an array or list to represent the four directions (North, East, South, West) as (dx, dy) pairs simplifies the turning logic.
  • Insight 2: Using a Hash Set for Obstacles: A hash set (or similar data structure for other languages like C++) provides efficient constant-time lookups to determine if a particular coordinate is an obstacle.
  • Insight 3: Step-by-Step Movement: Moving the robot one unit at a time within the move command allows for obstacle detection and immediate stopping if an obstacle is encountered.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(k)

Topics

This problem involves: Array, Hash Table, Simulation.

Companies

Asked at: Jane Street, PhonePe, Shopify.