java.lang.Object
g0801_0900.s0874_walking_robot_simulation.Solution

public class Solution extends Object
874 - Walking Robot Simulation.<p>Medium</p> <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> <ul> <li><code>-2</code>: Turn left <code>90</code> degrees.</li> <li><code>-1</code>: Turn right <code>90</code> 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 <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> <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> <p><strong>Note:</strong></p> <ul> <li>North means +Y direction.</li> <li>East means +X direction.</li> <li>South means -Y direction.</li> <li>West means -X direction.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> commands = [4,-1,3], obstacles = []</p> <p><strong>Output:</strong> 25</p> <p><strong>Explanation:</strong></p> <p>The robot starts at (0, 0):</p> <ol> <li> <p>Move north 4 units to (0, 4).</p> </li> <li> <p>Turn right.</p> </li> <li> <p>Move east 3 units to (3, 4).</p> </li> </ol> <p>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.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> commands = [4,-1,4,-2,4], obstacles = [[2,4]]</p> <p><strong>Output:</strong> 65</p> <p><strong>Explanation:</strong></p> <p>The robot starts at (0, 0):</p> <ol> <li> <p>Move north 4 units to (0, 4).</p> </li> <li> <p>Turn right.</p> </li> <li> <p>Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4).</p> </li> <li> <p>Turn left.</p> </li> <li> <p>Move north 4 units to (1, 8).</p> </li> </ol> <p>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.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> commands = [6,-1,-1,6], obstacles = []</p> <p><strong>Output:</strong> 36</p> <p><strong>Explanation:</strong></p> <p>The robot starts at (0, 0):</p> <ol> <li> <p>Move north 6 units to (0, 6).</p> </li> <li> <p>Turn right.</p> </li> <li> <p>Turn right.</p> </li> <li> <p>Move south 6 units to (0, 0).</p> </li> </ol> <p>The furthest point the robot ever gets from the origin is (0, 6), which squared is 6<sup>2</sup> = 36 units away.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= commands.length <= 10<sup>4</sup></code></li> <li><code>commands[i]</code> is either <code>-2</code>, <code>-1</code>, or an integer in the range <code>[1, 9]</code>.</li> <li><code>0 <= obstacles.length <= 10<sup>4</sup></code></li> <li><code>-3 * 10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 3 * 10<sup>4</sup></code></li> <li>The answer is guaranteed to be less than <code>2<sup>31</sup></code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • robotSim

      public int robotSim(int[] commands, int[][] obstacles)