| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.RobotBoundedInCircle; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class RobotBoundedInCircleSimulation : IRobotBoundedInCircle |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="instructions"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public bool IsRobotBounded(string instructions) |
| 4 | 24 | | { |
| 4 | 25 | | var direction = 0; |
| | 26 | |
|
| 4 | 27 | | var x = 0; |
| 4 | 28 | | var y = 0; |
| | 29 | |
|
| 50 | 30 | | foreach (var instruction in instructions) |
| 19 | 31 | | { |
| 19 | 32 | | switch (instruction) |
| | 33 | | { |
| | 34 | | case 'G': |
| 12 | 35 | | switch (direction) |
| | 36 | | { |
| | 37 | | case 0: |
| 6 | 38 | | y++; |
| | 39 | |
|
| 6 | 40 | | break; |
| | 41 | | case 1: |
| 1 | 42 | | x++; |
| | 43 | |
|
| 1 | 44 | | break; |
| | 45 | | case 2: |
| 4 | 46 | | y--; |
| | 47 | |
|
| 4 | 48 | | break; |
| | 49 | | case 3: |
| 1 | 50 | | x--; |
| | 51 | |
|
| 1 | 52 | | break; |
| | 53 | | } |
| | 54 | |
|
| 12 | 55 | | break; |
| | 56 | | case 'L': |
| 7 | 57 | | { |
| 7 | 58 | | direction = (direction + 3) % 4; |
| | 59 | |
|
| 7 | 60 | | break; |
| | 61 | | } |
| | 62 | | case 'R': |
| 0 | 63 | | { |
| 0 | 64 | | direction = (direction + 1) % 4; |
| | 65 | |
|
| 0 | 66 | | break; |
| | 67 | | } |
| | 68 | | } |
| 19 | 69 | | } |
| | 70 | |
|
| 4 | 71 | | return (x == 0 && y == 0) || direction != 0; |
| 4 | 72 | | } |
| | 73 | | } |