Walking Robot Simulation

Medium

Description

A robot on a grid follows commands to turn or move forward. Some grid cells have obstacles. Return max Euclidean distance squared from origin.

Examples

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

Robot at (3,4), distance² = 25.

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

Works with negative numbers.

Input:commands = [2,-2,8,-1,3], obstacles = [[1,-1],[2,-1],[3,-1]]
Output:36
Explanation:

The robot moves north 2 to (0,2), turns right to face east, moves 8 east to (8,2), turns left to face north, moves 3 north to (8,5). Obstacles at (1,-1), (2,-1), (3,-1) are not encountered. The maximum squared distance from origin is max(4, 68, 89) = 36 at some intermediate point.

Constraints

  • 1 ≤ commands.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!