Tap here to switch tabs
Problem
Submissions

Problem: Maximum Manhattan Distance After K Changes

med
30 min
Try to solve the Maximum Manhattan Distance After K Changes problem.

Statement

You are given a string s consisting of characters 'N', 'S', 'E', and 'W', which represents a sequence of movements on a 2extD2 ext{D} Cartesian plane starting from the origin (0,0)(0, 0). Each character corresponds to moving one unit in a specific direction:

  • 'N' moves one unit north to (x,y+1)(x, y + 1)

  • 'S' moves one unit south to (x,y−1)(x, y - 1)

  • 'E' moves one unit east to (x+1,y)(x + 1, y)

  • 'W' moves one unit west to (x−1,y)(x - 1, y)

You are also given an integer k. You can change at most k characters in s to any of the four directional characters.

Return the maximum Manhattan distance from the origin (0,0)(0, 0) that can be reached at any point while following the movements sequentially.

Note: The Manhattan distance between a point (x,y)(x, y) and the origin (0,0)(0, 0) is ∣x∣+∣y∣|x| + |y|.

Constraints:

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

  • 0≤0 \leq k ≤\leq s.length

  • s consists of only 'N', 'S', 'E', and 'W'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Manhattan Distance After K Changes

med
30 min
Try to solve the Maximum Manhattan Distance After K Changes problem.

Statement

You are given a string s consisting of characters 'N', 'S', 'E', and 'W', which represents a sequence of movements on a 2extD2 ext{D} Cartesian plane starting from the origin (0,0)(0, 0). Each character corresponds to moving one unit in a specific direction:

  • 'N' moves one unit north to (x,y+1)(x, y + 1)

  • 'S' moves one unit south to (x,y−1)(x, y - 1)

  • 'E' moves one unit east to (x+1,y)(x + 1, y)

  • 'W' moves one unit west to (x−1,y)(x - 1, y)

You are also given an integer k. You can change at most k characters in s to any of the four directional characters.

Return the maximum Manhattan distance from the origin (0,0)(0, 0) that can be reached at any point while following the movements sequentially.

Note: The Manhattan distance between a point (x,y)(x, y) and the origin (0,0)(0, 0) is ∣x∣+∣y∣|x| + |y|.

Constraints:

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

  • 0≤0 \leq k ≤\leq s.length

  • s consists of only 'N', 'S', 'E', and 'W'.