|
| 1 | +<h2><a href="https://leetcode.com/problems/walking-robot-simulation">874. Walking Robot Simulation</a></h2><h3>Medium</h3><hr><p>A robot on an infinite XY-plane starts at point <code>(0, 0)</code> facing north. The robot can receive a sequence of these three possible types of <code>commands</code>:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>-2</code>: Turn left <code>90</code> degrees.</li> |
| 5 | + <li><code>-1</code>: Turn right <code>90</code> degrees.</li> |
| 6 | + <li><code>1 <= k <= 9</code>: Move forward <code>k</code> units, one unit at a time.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Some of the grid squares are <code>obstacles</code>. The <code>i<sup>th</sup></code> obstacle is at grid point <code>obstacles[i] = (x<sub>i</sub>, y<sub>i</sub>)</code>. If the robot runs into an obstacle, then it will instead stay in its current location and move on to the next command.</p> |
| 10 | + |
| 11 | +<p>Return <em>the <strong>maximum Euclidean distance</strong> that the robot ever gets from the origin <strong>squared</strong> (i.e. if the distance is </em><code>5</code><em>, return </em><code>25</code><em>)</em>.</p> |
| 12 | + |
| 13 | +<p><strong>Note:</strong></p> |
| 14 | + |
| 15 | +<ul> |
| 16 | + <li>North means +Y direction.</li> |
| 17 | + <li>East means +X direction.</li> |
| 18 | + <li>South means -Y direction.</li> |
| 19 | + <li>West means -X direction.</li> |
| 20 | + <li>There can be obstacle in [0,0].</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong class="example">Example 1:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> commands = [4,-1,3], obstacles = [] |
| 28 | +<strong>Output:</strong> 25 |
| 29 | +<strong>Explanation:</strong> The robot starts at (0, 0): |
| 30 | +1. Move north 4 units to (0, 4). |
| 31 | +2. Turn right. |
| 32 | +3. Move east 3 units to (3, 4). |
| 33 | +The furthest point the robot ever gets from the origin is (3, 4), which squared is 3<sup>2</sup> + 4<sup>2</sup> = 25 units away. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> commands = [4,-1,4,-2,4], obstacles = [[2,4]] |
| 40 | +<strong>Output:</strong> 65 |
| 41 | +<strong>Explanation:</strong> The robot starts at (0, 0): |
| 42 | +1. Move north 4 units to (0, 4). |
| 43 | +2. Turn right. |
| 44 | +3. Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4). |
| 45 | +4. Turn left. |
| 46 | +5. Move north 4 units to (1, 8). |
| 47 | +The furthest point the robot ever gets from the origin is (1, 8), which squared is 1<sup>2</sup> + 8<sup>2</sup> = 65 units away. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p><strong class="example">Example 3:</strong></p> |
| 51 | + |
| 52 | +<pre> |
| 53 | +<strong>Input:</strong> commands = [6,-1,-1,6], obstacles = [] |
| 54 | +<strong>Output:</strong> 36 |
| 55 | +<strong>Explanation:</strong> The robot starts at (0, 0): |
| 56 | +1. Move north 6 units to (0, 6). |
| 57 | +2. Turn right. |
| 58 | +3. Turn right. |
| 59 | +4. Move south 6 units to (0, 0). |
| 60 | +The furthest point the robot ever gets from the origin is (0, 6), which squared is 6<sup>2</sup> = 36 units away. |
| 61 | +</pre> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | +<p><strong>Constraints:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li><code>1 <= commands.length <= 10<sup>4</sup></code></li> |
| 68 | + <li><code>commands[i]</code> is either <code>-2</code>, <code>-1</code>, or an integer in the range <code>[1, 9]</code>.</li> |
| 69 | + <li><code>0 <= obstacles.length <= 10<sup>4</sup></code></li> |
| 70 | + <li><code>-3 * 10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 3 * 10<sup>4</sup></code></li> |
| 71 | + <li>The answer is guaranteed to be less than <code>2<sup>31</sup></code>.</li> |
| 72 | +</ul> |
0 commit comments