Skip to main content
LeetCode 874, Medium. Topics: Array, Hash Table, Simulation. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

A robot on an infinite XY-plane starts at point <code>(0, 0)</code> facing north. The robot receives an array of integers <code>commands</code>, which represents a sequence of moves that it needs to execute. There are only three possible types of instructions the robot can receive:</p> <ul> <li><code>-2</code>: Turn left 90 degrees.</li> <li><code>-1</code>: Turn right 90 degrees.</li> <li><code>1 <= k <= 9</code>: Move forward <code>k</code> units, one unit at a time.</li> </ul> <p>Some of the grid squares are obstacles. The <code>ith</code> obstacle is at grid point <code>obstacles[i] = (xi, yi)</code>. If the robot runs into an obstacle, it will stay in its current location (on the block adjacent to the obstacle) and move onto the next command.</p> <p>Return <em>the maximum squared Euclidean distance that the robot reaches at any point in its path</em>.</p>

Examples

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.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026