leetcode - 3393. Count Paths With the Given XOR Value

Description

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

Solution

BFS/DFS (MLE)

Use bfs to generate all the possible paths, and check if the final number is equal to k.

Time complexity: o ( m ∗ n ) o(m*n) o(mn)
Space complexity: o ( m ∗ n ) o(m*n) o(mn)

DP

Solved after help… still don’t know why bfs would have memory limit error?

The transformation equation would be:
d p [ x ] [ y ] [ r e s ] = d p [ x − 1 ] [ y ] [ r e s ⊕ g r i d [ x ] [ y ] ] + d p [ x ] [ y − 1 ] [ r e s ⊕ g r i d [ x ] [ y ] ] dp[x][y][res] = dp[x - 1][y][res \oplus grid[x][y]] + dp[x][y - 1][res \oplus grid[x][y]] dp[x][y][res]=dp[x1][y][resgrid[x][y]]+dp[x][y1][resgrid[x][y]]
where d p [ x ] [ y ] [ r e s ] dp[x][y][res] dp[x][y][res] denotes the possible paths at (x,y) position, and the current XOR is res. Since res = previous_res ^ grid[x][y], so previous_res = res ^ grid[x][y].

Because of the constrain, the 3rd dimension is 16.
Time complexity: o ( m ∗ n ) o(m*n) o(mn)
Space complexity: o ( m ∗ n ) o(m*n) o(mn)

Code

BFS/DFS (MLE)

class Solution:
    def countPathsWithXorValue(self, grid: List[List[int]], k: int) -> int:
        queue = collections.deque([(0, 0, grid[0][0])])
        res = 0
        m, n = len(grid), len(grid[0])
        while queue:
            x, y, cur_res = queue.popleft()
            if x == m - 1 and y == n - 1 and cur_res == k:
                res += 1
                res %= 1000000007
                continue
            if 0 <= x + 1 < m and 0 <= y < n:
                queue.append((x + 1, y, cur_res ^ grid[x + 1][y]))
            if 0 <= x < m and 0 <= y + 1 < n:
                queue.append((x, y + 1, cur_res ^ grid[x][y + 1]))
        return res

DP

class Solution:
    def countPathsWithXorValue(self, grid: List[List[int]], k: int) -> int:
        m, n = len(grid), len(grid[0])
        dp = [[[-1] * 16 for _ in range(n)] for _ in range(m)]
        for xor in range(16):
            if xor == grid[0][0]:
                dp[0][0][xor] = 1
        for i in range(1, m):
            for xor in range(16):
                dp[i][0][xor] = dp[i - 1][0][xor ^ grid[i][0]]
        for j in range(1, n):
            for xor in range(16):
                dp[0][j][xor] = dp[0][j - 1][xor ^ grid[0][j]]
        for i in range(1, m):
            for j in range(1, n):
                for xor in range(16):
                    left = dp[i][j - 1][xor ^ grid[i][j]]
                    up = dp[i - 1][j][xor ^ grid[i][j]]
                    if left != -1 and up != -1:
                        dp[i][j][xor] = left + up
                    elif left != -1:
                        dp[i][j][xor] = left
                    elif up != -1:
                        dp[i][j][xor] = up
        return max(0, dp[m - 1][n - 1][k]) % 1000000007
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值