|
| 1 | +# Robot Bounded In Circle |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/robot-bounded-in-circle/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that: |
| 8 | + |
| 9 | +- The north direction is the positive direction of the y-axis. |
| 10 | +- The south direction is the negative direction of the y-axis. |
| 11 | +- The east direction is the positive direction of the x-axis. |
| 12 | +- The west direction is the negative direction of the x-axis. |
| 13 | + |
| 14 | +The robot can receive one of three instructions: |
| 15 | + |
| 16 | +- "G": go straight 1 unit. |
| 17 | +- "L": turn 90 degrees to the left (i.e., anti-clockwise direction). |
| 18 | +- "R": turn 90 degrees to the right (i.e., clockwise direction). |
| 19 | + |
| 20 | +The robot performs the instructions given in order, and repeats them forever. |
| 21 | + |
| 22 | +Return true if and only if there exists a circle in the plane such that the robot never leaves the circle. |
| 23 | + |
| 24 | +### Constraints |
| 25 | + |
| 26 | +- 1 <= instructions.length <= 100 |
| 27 | +- instructions[i] is 'G', 'L' or, 'R'. |
| 28 | + |
| 29 | +### Example |
| 30 | + |
| 31 | +``` |
| 32 | +Input: instructions = "GGLLGG" |
| 33 | +Output: true |
| 34 | +Explanation: The robot is initially at (0, 0) facing the north direction. |
| 35 | +"G": move one step. Position: (0, 1). Direction: North. |
| 36 | +"G": move one step. Position: (0, 2). Direction: North. |
| 37 | +"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West. |
| 38 | +"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South. |
| 39 | +"G": move one step. Position: (0, 1). Direction: South. |
| 40 | +"G": move one step. Position: (0, 0). Direction: South. |
| 41 | +Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0). |
| 42 | +Based on that, we return true. |
| 43 | +``` |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +- Can I cancel out the letters in a systematic way? |
| 48 | +- Other option is iterate thru string and update a location |
| 49 | + - if final location is (0,0) it's true |
| 50 | +- Vector addition? |
| 51 | +- If final direction is north and not loc (0,0) false |
| 52 | + |
| 53 | +### Pseudocode |
| 54 | + |
| 55 | +1. Create direction array |
| 56 | +2. Create current dir |
| 57 | +3. Create current loc |
| 58 | +4. Iterate thru string, update dir and loc |
| 59 | +5. If cur loc != 0,0 and cur dir = north return false |
| 60 | + |
| 61 | +### Initial Attempt |
| 62 | + |
| 63 | +```javascript |
| 64 | +const isRobotBounded = function (instructions) { |
| 65 | + const dir = ['N', 'E', 'S', 'W']; |
| 66 | + const dirMove = { |
| 67 | + N: [0, 1], |
| 68 | + E: [1, 0], |
| 69 | + S: [0, -1], |
| 70 | + W: [-1, 0], |
| 71 | + }; |
| 72 | + let curDir = 'N'; |
| 73 | + const curLoc = [0, 0]; |
| 74 | + |
| 75 | + for (i in instructions) { |
| 76 | + if (i === 'L') { |
| 77 | + } else if (i === 'R') { |
| 78 | + } else { |
| 79 | + curLoc = [curLoc[0] + dirMove[curDir][0], curLoc[1] + dirMove[curDir][1]]; |
| 80 | + } |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +### Optimized Solution |
| 86 | + |
| 87 | +Time complexity for this problem is O(n) and space complexity is O(1). You can see an explanation of the solution here: https://www.youtube.com/watch?v=nKv2LnC_g6E |
| 88 | + |
| 89 | +```javascript |
| 90 | +const isRobotBounded = function (instructions) { |
| 91 | + let [dirX, dirY] = [0, 1]; |
| 92 | + let [x, y] = [0, 0]; |
| 93 | + |
| 94 | + for (let i of instructions) { |
| 95 | + if (i === 'L') { |
| 96 | + [dirX, dirY] = [-dirY, dirX]; |
| 97 | + } else if (i === 'R') { |
| 98 | + [dirX, dirY] = [dirY, -dirX]; |
| 99 | + } else { |
| 100 | + [x, y] = [x + dirX, y + dirY]; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return (x === 0 && y === 0) || dirY !== 1; |
| 105 | +}; |
| 106 | +``` |
0 commit comments