Walking Robot Simulation


Latest problem from Leetcode is this one: https://leetcode.com/problems/walking-robot-simulation/description/ Now the description of the problem doesn’t seem super accurate since in reality what the author is asking is: as the robot walks, for each step there will be an Euclidean distance from that position to the origin. Return the maximum Euclidean distance to the origin as the robot walks the course. Also, it wasn’t clear that one could have duplicated obstacle. I want to believe that many Leetcoders must have been confused and that might be the reason that there are more thumb downs than ups. I failed submission few times because of these misunderstandings:


The problem is a simple traversal (some attention to details required) with usage of a hash table for a quick lookup into the obstacles. To quickly access the hash table, use the following trick to build the key: suppose that you want to build a key for index (a,b) with the following constraints:

0<=a<=N
0<=b<=N

All you need to generate a unique key is the following formula: key = a*SEED + b, and as long as SEED is a number greater than N (SEED > N), you’re guaranteed to generate a unique number based on any (a,b) - you can prove it mathematically.

After you add all the obstacles to the hash table using the above technique, just traverse the commands. Use a flag to determine the proper direction (some minor calculation needed), and as you “walk” the robot, decide whether it is an obstacle or not that you’re stepping on, and handle the situation accordingly. At each step, calculate the Euclidian distance to the origin, and keep track of the max. And that’s it! Code is down below, cheers, Marcelo.


       public class Solution
       {
             public int RobotSim(int[] commands, int[][] obstacles)
             {
                    Hashtable htObs = new Hashtable();
                    long seed = 40000L;
                    int retVal = 0;

                    for (int i = 0; i < obstacles.Length; i++)
                    {
                           long key = obstacles[i][0] * seed + obstacles[i][1];
                           if (!htObs.ContainsKey(key)) htObs.Add(key, true);
                    }

                    int x = 0;
                    int y = 0;
                    int direction = 0; //0 north, 1 west, 2 south, 3 east
                    for (int i = 0; i < commands.Length; i++)
                    {
                           if (commands[i] == -2)
                           {
                                  direction = (direction + 1) % 4;
                           }
                           else if (commands[i] == -1)
                           {
                                  direction--;
                                 if (direction < 0) direction = 3;
                           }
                           else
                           {
                                 int count = commands[i];
                                 while (count > 0)
                                 {
                                        if (x * x + y * y > retVal) retVal = x * x + y * y;

                                        int tx = x;
                                        int ty = y;
                                        switch (direction)
                                        {
                                              case 0:
                                                     y++;
                                                     break;
                                              case 1:
                                                     x--;
                                                     break;
                                              case 2:
                                                     y--;
                                                     break;
                                              case 3:
                                                     x++;
                                                     break;
                                        }

                                        long key = x * seed + y;
                                        if (htObs.ContainsKey(key))
                                        {
                                              //Revert back
                                              x = tx;
                                              y = ty;
                                              break;
                                        }

                                        if (x * x + y * y > retVal) retVal = x * x + y * y;

                                        count--;
                                 }
                           }
                    }

                    return retVal;
             }
       }

Comments

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Golang vs. C#: performance characteristics (simple case study)

Advent of Code - Day 7, 2024: Backtracking and Eval