3443. Maximum Manhattan Distance After K Changes
You are given a string
sconsisting of the characters'N','S','E', and'W', wheres[i]indicates movements in an infinite grid:
'N': Move north by 1 unit.'S': Move south by 1 unit.'E': Move east by 1 unit.'W': Move west by 1 unit.Initially, you are at the origin
(0, 0). You can change at mostkcharacters to any of the four directions.Find the maximum Manhattan distance from the origin that can be achieved at any time while performing the movements in order.
The Manhattan Distance between two cells
(xi, yi)and(xj, yj)is|xi - xj| + |yi - yj|.Example 1:
Input: s = "NWSE", k = 1
Output: 3
Explanation:
Change
s[2]from'S'to'N'. The stringsbecomes"NWNE".
Movement Position (x, y) Manhattan Distance Maximum s[0] == 'N' (0, 1) 0 + 1 = 1 1 s[1] == 'W' (-1, 1) 1 + 1 = 2 2 s[2] == 'N' (-1, 2) 1 + 2 = 3 3 s[3] == 'E' (0, 2) 0 + 2 = 2 3 The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.
Example 2:
Input: s = "NSWWEW", k = 3
Output: 6
Explanation:
Change
s[1]from'S'to'N', ands[4]from'E'to'W'. The stringsbecomes"NNWWWW".The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.
def maxDistance(self, s: str, k: int) -> int:
latitude = 0
longitude = 0
ans = 0
n = len(s)
for i in range(n):
if s[i] == "N":
latitude += 1
elif s[i] == "S":
latitude -= 1
elif s[i] == "E":
longitude += 1
elif s[i] == "W":
longitude -= 1
ans = max(ans, min(abs(latitude) + abs(longitude) + k * 2, i + 1))
return ans