The Glitchy Robot#12466

The Glitchy Robot (Editorial)

Let the final position of the robot without skipping any moves be . The Manhattan distance is |X| + |Y|.

When we skip a single move, say a move of type U (Up), the total displacement in the -direction decreases by 1. Thus, the new final position becomes (X, Y-1).

Since the order of vector addition is commutative, it does not matter which U we skip; the final result is identical. Therefore, we only need to check at most 4 scenarios:

  1. Skip one U (if present): New dist = |X| + |Y-1|.

  2. Skip one D (if present): New dist = |X| + |Y+1|.

  3. Skip one R (if present): New dist = |X-1| + |Y|.

  4. Skip one L (if present): New dist = |X+1| + |Y|.

We calculate the distance for the "No Skip" case and these 4 specific cases, then print the maximum. Time Complexity: to read the string, to check the 4 options.