Skip to main content

Minimum Bitwise OR From Grid - Video Solutions

MediumArrayGreedyBit ManipulationMatrix

Leetcode 3858 | Minimum Bitwise OR From Grid | Leetcode weekly contest 491

CodeWithMeGuys
18:401,489 views
8 video solutions available

Minimum Bitwise OR From Grid - Video Solution

Watch 8 video solutions for Minimum Bitwise OR From Grid, a medium level problem involving Array, Greedy, Bit Manipulation. This walkthrough by CodeWithMeGuys has 1,489 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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

You must select exactly one integer from each row of the grid.

Return an integer denoting the minimum possible bitwise OR of the selected integers from each row.

 

Example 1:

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

Output: 3

Explanation:

  • Choose 1 from the first row and 2 from the second row.
  • The bitwise OR of 1 | 2 = 3​​​​​​​, which is the minimum possible.

Example 2:

Input: grid = [[3,5],[6,4]]

Output: 5

Explanation:

  • Choose 5 from the first row and 4 from the second row.
  • The bitwise OR of 5 | 4 = 5​​​​​​​, which is the minimum possible.

Example 3:

Input: grid = [[7,9,8]]

Output: 7

Explanation:

  • Choosing 7 gives the minimum bitwise OR.

 

Constraints:

  • 1 <= m == grid.length <= 105
  • 1 <= n == grid[i].length <= 105
  • m * n <= 105
  • 1 <= grid[i][j] <= 105​​​​​​​
Read full problem with examples

Approach Overview

Problem Overview: You are given an m x n grid where each cell contains an integer. Starting from the top-left cell, reach the bottom-right cell while minimizing the bitwise OR of all values along the chosen path. Movement is typically restricted to valid adjacent cells in the matrix. The challenge is that once a bit becomes 1 in the OR result, it cannot be reverted.

Approach 1: Dijkstra with Bitwise OR State (O(m*n log(m*n)) time, O(m*n) space)

Treat the grid as a weighted graph. Each move to a neighboring cell produces a new cost equal to current_cost | grid[nr][nc]. Use a priority queue similar to Dijkstra's algorithm to always expand the state with the smallest OR value so far. Maintain a distance matrix storing the smallest OR seen for each cell and skip states that produce larger values. This approach works because the OR operation is monotonic: costs never decrease as you extend the path.

The method is straightforward and reliable for moderate grid sizes. It resembles classic shortest path problems and uses common structures like priority queues and distance arrays.

Approach 2: Greedy Bit Filtering + BFS (O(32 * m * n) time, O(m*n) space)

The optimal approach leverages bit manipulation and a greedy observation. The OR result is determined bit by bit. Higher bits contribute more to the final value, so attempt to keep them 0 whenever possible.

Iterate bits from the most significant (for example 31) down to 0. For each bit, temporarily forbid stepping on cells whose value contains that bit (grid[i][j] & (1 << bit)). Run a BFS/DFS to check if the bottom-right cell is still reachable. If a path exists, that bit can remain 0 in the final answer. If no path exists, the bit is unavoidable, so add it to the result and allow those cells again.

This works because the OR operation accumulates bits irreversibly. Deciding higher bits first ensures the smallest possible final integer. The reachability check is simply a grid traversal using techniques from matrix problems and greedy algorithms.

Recommended for interviews: The greedy bit-by-bit filtering approach is what most interviewers expect. It shows understanding of how bitwise OR behaves and reduces the search space to at most 32 reachability checks. Mentioning the Dijkstra formulation first demonstrates baseline graph thinking, while implementing the greedy method shows stronger optimization skills.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Dijkstra with Bitwise OR CostO(m*n log(m*n))O(m*n)General graph interpretation of the grid; easiest to reason about
Greedy Bit Filtering + BFSO(32*m*n)O(m*n)Optimal approach when minimizing OR value bit by bit