Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Manhattan Distance After K Changes

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3443" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a string s consisting of the characters 'N', 'S', 'E', and 'W', where s[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 most k characters 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 string s becomes "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', and s[4] from 'E' to 'W'. The string s becomes "NNWWWW". The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output. Constraints: 1 <= s.length <= 105 0 <= k <= s.length s consists of only 'N', 'S', 'E', and 'W'.

Explanation

Here's the approach, complexity, and code:

  • Dynamic Programming: Use dynamic programming to track the maximum east-west (x) and north-south (y) distances achievable at each step, considering the changes allowed.
  • State Representation: dp[i][j][0] stores the maximum east-west distance at index i with j changes made, and dp[i][j][1] stores the corresponding maximum north-south distance.
  • Maximization: At each step, explore all possibilities (N, S, E, W) whether changing the character or not, and update the dp table to maximize distances.

  • Runtime Complexity: O(n*k), where n is the length of the string and k is the maximum number of changes allowed. Storage Complexity: O(n*k).

Code

    def max_manhattan_distance(s: str, k: int) -> int:
    """
    Finds the maximum Manhattan distance from the origin that can be achieved after movements,
    given a maximum number of changes allowed.
    """

    n = len(s)
    dp = [[[-(10**9), -(10**9)] for _ in range(k + 1)] for _ in range(n + 1)]  # Initialize with a very small value

    dp[0][0][0] = 0
    dp[0][0][1] = 0

    for i in range(1, n + 1):
        for j in range(k + 1):
            # Case 1: No change
            if s[i - 1] == 'N':
                dp[i][j][0] = dp[i-1][j][0]
                dp[i][j][1] = dp[i-1][j][1] + 1
            elif s[i - 1] == 'S':
                dp[i][j][0] = dp[i - 1][j][0]
                dp[i][j][1] = dp[i - 1][j][1] - 1
            elif s[i - 1] == 'E':
                dp[i][j][0] = dp[i - 1][j][0] + 1
                dp[i][j][1] = dp[i - 1][j][1]
            else:  # s[i - 1] == 'W'
                dp[i][j][0] = dp[i - 1][j][0] - 1
                dp[i][j][1] = dp[i - 1][j][1]

            # Case 2: Change the character (if changes are available)
            if j > 0:
                dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - 1][0] + 1 if s[i-1] != 'E' else -(10**9))  # Change to E
                dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - 1][0] - 1 if s[i-1] != 'W' else -(10**9))  # Change to W
                dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - 1][1] + 1 if s[i-1] != 'N' else -(10**9))  # Change to N
                dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - 1][1] - 1 if s[i-1] != 'S' else -(10**9))  # Change to S

    max_dist = 0
    for j in range(k + 1):
        max_dist = max(max_dist, abs(dp[n][j][0]) + abs(dp[n][j][1]))

    return max_dist

More from this blog

C

Chatmagic blog

2894 posts