Skip to content

Files

Latest commit

e1abef0 · Mar 17, 2020

History

History
119 lines (46 loc) · 2.08 KB

File metadata and controls

119 lines (46 loc) · 2.08 KB

Description

A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of three possible types of commands:

    <li><code>-2</code>: turn left 90 degrees</li>
    
    <li><code>-1</code>: turn right 90 degrees</li>
    
    <li><code>1 &lt;= x &lt;= 9</code>: move forward <code>x</code> units</li>
    

Some of the grid squares are obstacles. 

The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])

If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)

Return the square of the maximum Euclidean distance that the robot will be from the origin.

 

Example 1:

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

Output: 25

Explanation: robot will go to (3, 4)

Example 2:

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

Output: 65

Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)

 

Note:

    <li><code>0 &lt;= commands.length &lt;= 10000</code></li>
    
    <li><code>0 &lt;= obstacles.length &lt;= 10000</code></li>
    
    <li><code>-30000 &lt;= obstacle[i][0] &lt;= 30000</code></li>
    
    <li><code>-30000 &lt;= obstacle[i][1] &lt;= 30000</code></li>
    
    <li>The answer is guaranteed to be less than <code>2 ^ 31</code>.</li>
    

Solutions

Python3

Java

...