Skip to content

3443. Maximum Manhattan Distance After K Changes #1830

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the maximum Manhattan distance from the origin (0, 0) achievable at any point during the sequence of movements, given that we can change at most k characters in the string s to any of the four directions ('N', 'S', 'E', 'W'). The Manhattan distance is defined as the sum of the absolute differences in the x and y coordinates, i.e., |x| + |y|.

Approach

  1. Initialization: Start at the origin (0, 0). Initialize variables to track the current position (x, y) and the maximum Manhattan distance encountered (ans).
  2. Iterate through the string: For each character in the string, update the current position (x, y) based on the movement direction:
    • 'N' increments the y-coordinate.
    • 'S' dec…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Jun 20, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jun 20, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants