Menu

3882. Minimum XOR Path in a Grid
πŸ“°

3882. Minimum XOR Path in a Grid

DEV Community: phpΒ·MD ARIFUL HAQUEΒ·about 9 hours ago
#6rozO6EX
#devto#dev#valid#paths#grid
Reading 0:00
15s threshold

3882. Minimum XOR Path in a Grid

Difficulty: Medium

Topics: Staff , Array , Dynamic Programming , Bit Manipulation , Matrix , Biweekly Contest 179

You are given a 2D integer array grid of size m * n .

You start at the top-left cell (0, 0) and want to reach the bottom-right cell (m - 1, n - 1) .

At each step, you may move either right or down .

The cost of a path is defined as the bitwise XOR of all the values in the cells along that path, including the start and end cells.

Return the minimum possible XOR value among all valid paths from (0, 0) to (m - 1, n - 1) .

Example 1:

Input: grid = [[1,2],[3,4]]

Output: 6

Explanation:

There are two valid paths:

(0, 0) β†’ (0, 1) β†’ (1, 1) with XOR: 1 XOR 2 XOR 4 = 7

(0, 0) β†’ (1, 0) β†’ (1, 1) with XOR: 1 XOR 3 XOR 4 = 6

The minimum XOR value among all valid paths is 6.

Example 2:

Input: grid = [[6,7],[5,8]]

Output: 9

Explanation:

There are two valid paths:

(0, 0) β†’ (0, 1) β†’ (1, 1) with XOR: 6 XOR 7 XOR 8 = 9

(0, 0) β†’ (1, 0) β†’ (1, 1) with XOR: 6 XOR 5…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.