1 line
3.9 KiB
Plaintext
1 line
3.9 KiB
Plaintext
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Robot in a circle (1041).ipynb","provenance":[],"mount_file_id":"1N6CVDK8RDLCw51gS-qTRMVTM2LUAoUSd","authorship_tag":"ABX9TyMfx0mRceDnoVgQg541ZY3a"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# 1041. Robot in a Circle\n","\n","On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:\n","The north direction is the positive direction of the y-axis.\n","The south direction is the negative direction of the y-axis.\n","The east direction is the positive direction of the x-axis.\n","The west direction is the negative direction of the x-axis.\n","The robot can receive one of three instructions:\n","\"G\": go straight 1 unit.\n","\"L\": turn 90 degrees to the left (i.e., anti-clockwise direction).\n","\"R\": turn 90 degrees to the right (i.e., clockwise direction).\n","The robot performs the instructionsgiven in order, and repeats them forever.\n","Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.\n"," \n","Example 1:\n","Input: instructions = \"GGLLGG\"\n","Output: true\n","Explanation: The robot is initially at (0, 0) facing the north direction.\n","\"G\": move one step. Position: (0, 1). Direction: North.\n","\"G\": move one step. Position: (0, 2). Direction: North.\n","\"L\": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.\n","\"L\": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.\n","\"G\": move one step. Position: (0, 1). Direction: South.\n","\"G\": move one step. Position: (0, 0). Direction: South.\n","Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).\n","Based on that, we return true.\n","\n","Example 2:\n","Input: instructions = \"GG\"\n","Output: false\n","Explanation: The robot is initially at (0, 0) facing the north direction.\n","\"G\": move one step. Position: (0, 1). Direction: North.\n","\"G\": move one step. Position: (0, 2). Direction: North.\n","Repeating the instructions, keeps advancing in the north direction and does not go into cycles.\n","Based on that, we return false.\n","\n","Example 3:\n","Input: instructions = \"GL\"\n","Output: true\n","Explanation: The robot is initially at (0, 0) facing the north direction.\n","\"G\": move one step. Position: (0, 1). Direction: North.\n","\"L\": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.\n","\"G\": move one step. Position: (-1, 1). Direction: West.\n","\"L\": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.\n","\"G\": move one step. Position: (-1, 0). Direction: South.\n","\"L\": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.\n","\"G\": move one step. Position: (0, 0). Direction: East.\n","\"L\": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.\n","Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).\n","Based on that, we return true.\n","\n"," \n","Constraints:\n","1 <= instructions.length <= 100\n","instructions[i] is 'G', 'L'\n"],"metadata":{"id":"XiAC8FSd7ezE"}},{"cell_type":"code","execution_count":5,"metadata":{"id":"s3jO-ovb7K4m","executionInfo":{"status":"ok","timestamp":1656786251777,"user_tz":240,"elapsed":219,"user":{"displayName":"Shwetha Jayaraj","userId":"01455478857425759475"}}},"outputs":[],"source":["class Solution:\n"," def isRobotBounded(self, instructions: str) -> bool:\n"," x = 0\n"," y = 0\n"," d = 0\n"," directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]\n","\n"," for instruction in instructions:\n"," if instruction == 'G':\n"," x += directions[d][0]\n"," y += directions[d][1]\n"," elif instruction == 'L':\n"," d = (d + 3) % 4\n"," else:\n"," d = (d + 1) % 4\n","\n"," return (x, y) == (0, 0) or d > 0\n"]}]} |