Solving Paint a Boolean Matrix
In this article we will look to solve another classic graph and matrix problem, painting a boolean matrix.
Problem statement
The 2 colors will be represented by 0’s and 1’s.
For this example, we will start in the center of the array or [1,1]
. Since we are starting in the center we will only be able to flip the upper, leftmost triangular matrix.
It’s easier to understand by seeing it. See below:
Understanding the problem
We can use a BFS approach to traverse the matrix.
Like in any matrix problem will need to check that we are within bounds of the matrix. We would also need to check the new position is colored the same as the previous position. If the new position fits the requirements, its color is flipped.
Our time complexity for this would be O(mn)
Pseudocode
Code
Like in any matrix problem, its all about making sure you stay within the bounds while you are solving the problem.