Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Paths With the Given XOR Value

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3393" 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 2D integer array grid with size m x n. You are also given an integer k. Your task is to calculate the number of paths you can take from the top-left cell (0, 0) to the bottom-right cell (m - 1, n - 1) satisfying the following constraints: You can either move to the right or down. Formally, from the cell (i, j) you may move to the cell (i, j + 1) or to the cell (i + 1, j) if the target cell exists. The XOR of all the numbers on the path must be equal to k. Return the total number of such paths. Since the answer can be very large, return the result modulo 109 + 7. Example 1: Input: grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11 Output: 3 Explanation: The 3 paths are: (0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) (0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2) (0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2) Example 2: Input: grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2 Output: 5 Explanation: The 5 paths are: (0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3) (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3) (0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3) (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3) (0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3) Example 3: Input: grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10 Output: 0 Constraints: 1 <= m == grid.length <= 300 1 <= n == grid[r].length <= 300 0 <= grid[r][c] < 16 0 <= k < 16

Explanation

Here's the solution to the problem:

  • Dynamic Programming: The core idea is to use dynamic programming to store the number of paths to reach a cell (i, j) with a given XOR value.
  • State: dp[i][j][xor] represents the number of paths from (0, 0) to (i, j) with an XOR value of xor.
  • Transition: The number of paths to dp[i][j][xor] is the sum of paths coming from top dp[i-1][j][xor ^ grid[i][j]] and left dp[i][j-1][xor ^ grid[i][j]]

  • Runtime Complexity: O(m n k), where m and n are the dimensions of the grid, and k is the target XOR value.

  • Storage Complexity: O(m n k)

Code

    def numberOfPaths(grid, k):
    m, n = len(grid), len(grid[0])
    MOD = 10**9 + 7

    dp = [[[0] * 16 for _ in range(n)] for _ in range(m)]

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

    for i in range(m):
        for j in range(n):
            for xor in range(16):
                if i > 0:
                    dp[i][j][grid[i][j] ^ xor] = (dp[i][j][grid[i][j] ^ xor] + dp[i-1][j][xor]) % MOD
                if j > 0:
                    dp[i][j][grid[i][j] ^ xor] = (dp[i][j][grid[i][j] ^ xor] + dp[i][j-1][xor]) % MOD

    return dp[m-1][n-1][k]

More from this blog

C

Chatmagic blog

2894 posts