Skip to content

Practice Walking Robot Simulation

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#874
LeetCode Problem

Walking Robot Simulation

Problem Statement

A robot begins at (0, 0) facing north, the positive y direction. Command -2 turns left 90 degrees, -1 turns right 90 degrees, and an integer from 1 through 9 moves that many unit steps forward. Stop the current move before entering an obstacle and then process the next command. An obstacle at the origin does not prevent departure, but prevents a later return. Return the maximum x^2 + y^2 reached anywhere along the path.

Source: Walking Robot Simulation on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
commands = [4,-1,3], obstacles = []
Output
25

# Example 2

Input
commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output
65

# Example 3

Input
commands = [6,-1,-1,6], obstacles = [[0,0]]
Output
36

Constraints

1 <= commands.length <= 10^4
commands[i] is either -2, -1, or an integer in the range [1, 9].
0 <= obstacles.length <= 10^4
-3 * 10^4 <= xi, yi <= 3 * 10^4
The answer is guaranteed to be less than 2^31.

Related LeetCode Problems

Practice more problems similar to Walking Robot Simulation.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.