You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
"G": go straight 1 unit;
"L": turn 90 degrees to the left;
"R": turn 90 degrees to the right.
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.
/** * @param {string} instructions * @return {boolean} */// T.C: O(N)// S.C: O(1)varisRobotBounded=function(instructions){letx=0,y=0;// Up(north), right, down(south), leftconstdirs=[[0,1],[1,0],[0,-1],[-1,0]];// by default, a robot is facing northleti=0;for(letj=0;j<instructions.length;j++){// if you turn, then the "new" left and right is based on below formulaif(instructions[j]==='R')i=(i+1)%4;elseif(instructions[j]==='L')i=(i+3)%4;else{// if G, follows the direction and movex+=dirs[i][0];y+=dirs[i][1];}}// if i > 0, eventually will go back to the original pointreturn(x===0&&y===0)||i>0;};