Skip to content

Practice Disconnect Path in a Binary Matrix by at Most One Flip

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2556
LeetCode Problem

Disconnect Path in a Binary Matrix by at Most One Flip

Problem Statement

You are given a 0-indexed m x n binary matrix grid. You can move from a cell (row, col) to any of the cells (row + 1, col) or (row, col + 1) that has the value 1. The matrix is disconnected if there is no path from (0, 0) to (m - 1, n - 1). You can flip the value of at most one (possibly none) cell. You cannot flip the cells (0, 0) and (m - 1, n - 1). Return true if it is possible to make the matrix disconnect or false otherwise. Note that flipping a cell changes its value from 0 to 1 or from 1 to 0.

Source: Disconnect Path in a Binary Matrix by at Most One Flip on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[1,1,1],[1,0,0],[1,1,1]]
Output
true

# Example 2

Input
grid = [[1,1,1],[1,0,1],[1,1,1]]
Output
false

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 1000
1 <= m * n <= 10^5
grid[i][j] is either 0 or 1.
grid[0][0] == grid[m - 1][n - 1] == 1

Related LeetCode Problems

Practice more problems similar to Disconnect Path in a Binary Matrix by at Most One Flip.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.