💻
LeetCode -> NeetCode
  • Leetcode
  • 1. Two Sum
    • CODE
  • 100. Same Tree
    • CODE
  • 102. Binary Tree Level Order Traversal
    • CODE
  • 103. Binary Tree Zigzag Level Order Traversal
    • CODE
  • 104. Maximum Depth of Binary Tree
    • CODE
  • 1041. Robot Bounded In Circle
    • CODE
  • 1046. Last Stone Weight
    • CODE
  • 105. Construct Binary Tree from Preorder and Inorder Traversal
    • CODE
  • 11. Container With Most Water
    • CODE
  • 110. Balanced Binary Tree
    • CODE
  • 111. Minimum Depth of Binary Tree
    • CODE
  • 112. Path Sum
    • CODE
  • 1143. Longest Common Subsequence
    • CODE
  • 116. Populating Next Right Pointers in Each Node
    • CODE
  • 121. Best Time to Buy and Sell Stock
    • CODE
  • 124. Binary Tree Maximum Path Sum
    • CODE
  • 125. Valid Palindrome
    • CODE
  • 128. Longest Consecutive Sequence
    • CODE
  • 130. Surrounded Regions
    • CODE
  • 131-palindrome-partitioning
    • CODE
  • 133. Clone Graph
    • CODE
  • 134. Gas Station
    • CODE
  • 136. Single Number
    • CODE
  • 138. Copy List with Random Pointer
    • CODE
  • 139. Word Break
    • CODE
  • 141. Linked List Cycle
    • CODE
  • 143. Reorder List
    • CODE
  • 1448. Count Good Nodes in Binary Tree
    • CODE
  • 146. LRU Cache
    • CODE
  • 1481. Least Number of Unique Integers after K Removals
    • CODE
  • 15. 3Sum
    • CODE
  • 150. Evaluate Reverse Polish Notation
    • CODE
  • 152. Maximum Product Subarray
    • CODE
  • 153. Find Minimum in Rotated Sorted Array
    • CODE
  • 1539. Kth Missing Positive Number
    • CODE
  • 155. Min Stack
    • CODE
  • 1584. Min Cost to Connect All Points
    • CODE
  • 162. Find Peak Element
    • CODE
  • 167. Two Sum II - Input Array Is Sorted
    • CODE
  • 17. Letter Combinations of a Phone Number
    • CODE
  • 1834. Single-Threaded CPU
    • CODE
  • 1899-merge-triplets-to-form-target-triplet
    • CODE
  • 19. Remove Nth Node From End of List
    • CODE
  • 190. Reverse Bits
    • CODE
  • 191. Number of 1 Bits
    • CODE
  • 198. House Robber
    • CODE
  • 199. Binary Tree Right Side View
    • CODE
  • 2. Add Two Numbers
    • CODE
  • 20. Valid Parentheses
    • CODE
  • 200. Number of Islands
    • CODE
  • 2013. Detect Squares
    • CODE
  • 202. Happy Number
    • CODE
  • 2055. Plates Between Candles
    • CODE
  • 206. Reverse Linked List
    • CODE
  • 207. Course Schedule
    • CODE
  • 208. Implement Trie (Prefix Tree)
    • CODE
  • 21. Merge Two Sorted Lists
    • CODE
  • 211. Design Add and Search Words Data Structure
    • CODE
  • 212. Word Search II
    • CODE
  • 213. House Robber II
    • CODE
  • 215. Kth Largest Element in an Array
    • CODE
  • 217. Contains Duplicate
    • CODE
  • 226. Invert Binary Tree
    • CODE
  • 23. Merge k Sorted Lists
    • CODE
  • 230. Kth Smallest Element in a BST
    • CODE
  • 235. Lowest Common Ancestor of a Binary Search Tree
    • CODE
  • 238. Product of Array Except Self
    • CODE
  • 239. Sliding Window Maximum
    • CODE
  • 242. Valid Anagram
    • CODE
  • 252-meeting-rooms
    • CODE
  • 253-meeting-rooms-ii
    • CODE
  • 268. Missing Number
    • CODE
  • 287. Find the Duplicate Number
    • CODE
  • 295. Find Median from Data Stream
    • CODE
  • 297. Serialize and Deserialize Binary Tree
    • CODE
  • 3. Longest Substring Without Repeating Characters
    • CODE
  • 300. Longest Increasing Subsequence
    • CODE
  • 322. Coin Change
    • CODE
  • 33. Search in Rotated Sorted Array
    • CODE
  • 338. Counting Bits
    • CODE
  • 347. Top K Frequent Elements
    • CODE
  • 35. Search Insert Position
    • CODE
  • 355. Design Twitter
    • CODE
  • 36. Valid Sudoku
    • CODE
  • 371. Sum of Two Integers
    • CODE
  • 378. Kth Smallest Element in a Sorted Matrix
    • CODE
  • 39. Combination Sum
    • CODE
  • 4. Median of Two Sorted Arrays
    • CODE
  • 40. Combination Sum II
    • CODE
  • 417-pacific-atlantic-water-flow
    • CODE
  • 42-trapping-rain-water
    • CODE
  • 424. Longest Repeating Character Replacement
    • CODE
  • 43. Multiply Strings
    • CODE
  • 435. Non-overlapping Intervals
    • CODE
  • 436. Find Right Interval
    • CODE
  • 442. Find All Duplicates in an Array
    • CODE
  • 448. Find All Numbers Disappeared in an Array
    • CODE
  • 45. Jump Game II
    • CODE
  • 46. Permutations
    • CODE
  • 47. Permutations II
    • CODE
  • 48. Rotate Image
    • CODE
  • 49. Group Anagrams
    • CODE
  • 5. Longest Palindromic Substring
    • CODE
  • 50. Pow(x, n)
    • CODE
  • 51. N-Queens
    • CODE
  • 53. Maximum Subarray
    • CODE
  • 54. Spiral Matrix
    • CODE
  • 543. Diameter of Binary Tree
    • CODE
  • 547. Number of Provinces
    • CODE
  • 55. Jump Game
    • CODE
  • 554. Brick Wall
    • CODE
  • 56. Merge Intervals
    • CODE
  • 567. Permutation in String
    • CODE
  • 57. Insert Interval
    • CODE
  • 572. Subtree of Another Tree
    • CODE
  • 581. Shortest Unsorted Continuous Subarray
    • CODE
  • 61. Rotate List
    • CODE
  • 62. Unique Paths
    • CODE
  • 621. Task Scheduler
    • CODE
  • 647. Palindromic Substrings
    • CODE
  • 659 · Encode and Decode Strings
    • CODE
  • 66. Plus One
    • CODE
  • 695. Max Area of Island
    • CODE
  • 7. Reverse Integer
    • CODE
  • 70. Climbing Stairs
    • CODE
  • 703. Kth Largest Element in a Stream
    • CODE
  • 704. Binary Search
    • CODE
  • 73-set-matrix-zeroes
    • CODE
  • 739. Daily Temperatures
    • CODE
  • 74. Search a 2D Matrix
    • CODE
  • 746. Min Cost Climbing Stairs
    • CODE
  • 75. Sort Colors
    • CODE
  • 76-minimum-window-substring
    • CODE
  • 763. Partition Labels
    • CODE
  • 78. Subsets
    • CODE
  • 784. Letter Case Permutation
    • CODE
  • 79. Word Search
    • CODE
  • 796. Rotate String
    • CODE
  • 828. Count Unique Characters of All Substrings of a Given String
    • CODE
  • 832. Flipping an Image
    • CODE
  • 84. Largest Rectangle in Histogram
    • CODE
  • 846. Hand of Straights
    • CODE
  • 853. Car Fleet
  • 875. Koko Eating Bananas
    • CODE
  • 90. Subsets II
    • CODE
  • 904. Fruit Into Baskets
    • CODE
  • 91. Decode Ways
    • CODE
  • 973. K Closest Points to Origin
    • CODE
  • 977. Squares of a Sorted Array
    • CODE
  • 98. Validate Binary Search Tree
    • CODE
  • 981-time-based-key-value-store
    • CODE
  • 993. Cousins in Binary Tree
    • CODE
  • 994. Rotting Oranges
    • CODE
  • scripts
    • CODE
Powered by GitBook
On this page
Edit on GitHub

1041. Robot Bounded In Circle

Medium


On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:

  • The north direction is the positive direction of the y-axis.

  • The south direction is the negative direction of the y-axis.

  • The east direction is the positive direction of the x-axis.

  • The west direction is the negative direction of the x-axis.

The robot can receive one of three instructions:

  • "G": go straight 1 unit.

  • "L": turn 90 degrees to the left (i.e., anti-clockwise direction).

  • "R": turn 90 degrees to the right (i.e., clockwise direction).

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:

Input: instructions = "GGLLGG"
Output:
 true
Explanation:
 The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
"G": move one step. Position: (0, 1). Direction: South.
"G": move one step. Position: (0, 0). Direction: South.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
Based on that, we return true.

Example 2:

Input: instructions = "GG"
Output:
 false
Explanation:
 The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
Based on that, we return false.

Example 3:

Input: instructions = "GL"
Output:
 true
Explanation:
 The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
"G": move one step. Position: (-1, 1). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
"G": move one step. Position: (-1, 0). Direction: South.
"L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
"G": move one step. Position: (0, 0). Direction: East.
"L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
Based on that, we return true.

Constraints:

  • 1 <= instructions.length <= 100

  • instructions[i] is 'G', 'L' or, 'R'.

PreviousCODENextCODE

Last updated 2 years ago