Maximum Manhattan Distance After K Changes - Problem

Imagine you're controlling a robot on an infinite grid, starting at the origin (0, 0). You have a sequence of movement commands represented by a string s where each character tells the robot which direction to move:

  • 'N': Move north (up) by 1 unit
  • 'S': Move south (down) by 1 unit
  • 'E': Move east (right) by 1 unit
  • 'W': Move west (left) by 1 unit

Here's the twist: before executing the movement sequence, you can modify at most k characters in the string to any of the four directions. Your goal is to maximize the Manhattan distance from the origin that the robot reaches at any point during its journey.

The Manhattan distance between two points (x₁, y₁) and (x₂, y₂) is |x₁ - x₂| + |y₁ - y₂|.

Key insight: You want to strategically change some moves to make the robot travel as far as possible from the origin at some point during its path, not necessarily at the end.

Input & Output

example_1.py — Basic Example
$ Input: s = "NESE", k = 1
Output: 4
💡 Note: Original path: (0,0)→(0,1)→(1,1)→(1,0)→(2,0), max distance = 2. Change S to N: (0,0)→(0,1)→(1,1)→(1,2)→(2,2), max distance = 4.
example_2.py — Multiple Changes
$ Input: s = "NSEW", k = 2
Output: 4
💡 Note: Original path reaches max distance 1. Change N→E and S→E to get "EEEW": (0,0)→(1,0)→(2,0)→(3,0)→(2,0), max distance = 3. Better: change N→E and W→E to get "ESEE": (0,0)→(1,0)→(1,-1)→(2,-1)→(3,-1), max distance = 4.
example_3.py — No Changes Needed
$ Input: s = "NNNN", k = 0
Output: 4
💡 Note: Already optimal path going straight North: (0,0)→(0,1)→(0,2)→(0,3)→(0,4), max distance = 4.

Visualization

Tap to expand
Robot Path Optimization VisualizationOriginal Path (NESE)Start(0,1)(1,1)(1,0)(2,0)Max distance: 2Optimized Path (NENE)Start(0,1)(1,1)(1,2)(2,2) MAX!Max distance: 4Key Insight:• Calculate prefix sums to track position after each move• Evaluate each possible change's impact on maximum distance• Greedily select k changes that provide maximum improvement• Changed S→N at position 2 increases max distance from 2 to 4
Understanding the Visualization
1
Track Original Path
Calculate where the robot would be after each move using prefix sums
2
Evaluate Modifications
For each move, consider changing it and calculate the distance improvement
3
Select Best Changes
Greedily choose the k modifications that provide maximum distance gain
4
Simulate Final Path
Execute the optimized sequence and find the maximum distance reached
Key Takeaway
🎯 Key Insight: Use prefix sums to efficiently calculate position changes and greedily select modifications that maximize the furthest distance reached during the journey, not just the final position.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

O(n) to calculate prefix sums, O(n log n) to sort potential improvements

n
2n
Linearithmic
Space Complexity
O(n)

Space for storing prefix sums and improvement candidates

n
2n
Linearithmic Space

Constraints

  • 1 ≤ s.length ≤ 105
  • 0 ≤ k ≤ s.length
  • s consists only of characters 'N', 'S', 'E', 'W'
  • The string represents a valid sequence of moves
Asked in
Google 42 Meta 35 Amazon 28 Microsoft 22
47.2K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen