Tap here to switch tabs
Problem
Submissions

Problem: Maximum Manhattan Distance After All Moves

Try to solve the Maximum Manhattan Distance After All Moves problem.

Statement

You are given a string moves representing a sequence of moves starting at the origin (0,0)(0, 0) on a Cartesian plane.

Each character in moves specifies a direction:

  • 'U': Move 11 unit up to (x,y+1)(x, y + 1)

  • 'D': Move 11 unit down to (x,y−1)(x, y - 1)

  • 'L': Move 11 unit left to (x−1,y)(x - 1, y)

  • 'R': Move 11 unit right to (x+1,y)(x + 1, y)

  • '_': Can be replaced by any one of the four moves 'U', 'D', 'L', or 'R'

Return the maximum Manhattan distance from the origin (0,0)(0, 0) to the final position after executing all moves.

Note: The Manhattan distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

Constraints:

  • 1≤1 \leq moves.length ≤105\leq 10^5

  • moves consists only of 'U', 'D', 'L', 'R', and '_'

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Manhattan Distance After All Moves

Try to solve the Maximum Manhattan Distance After All Moves problem.

Statement

You are given a string moves representing a sequence of moves starting at the origin (0,0)(0, 0) on a Cartesian plane.

Each character in moves specifies a direction:

  • 'U': Move 11 unit up to (x,y+1)(x, y + 1)

  • 'D': Move 11 unit down to (x,y−1)(x, y - 1)

  • 'L': Move 11 unit left to (x−1,y)(x - 1, y)

  • 'R': Move 11 unit right to (x+1,y)(x + 1, y)

  • '_': Can be replaced by any one of the four moves 'U', 'D', 'L', or 'R'

Return the maximum Manhattan distance from the origin (0,0)(0, 0) to the final position after executing all moves.

Note: The Manhattan distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

Constraints:

  • 1≤1 \leq moves.length ≤105\leq 10^5

  • moves consists only of 'U', 'D', 'L', 'R', and '_'