Given a binary matrix mat having dimensions N * M and a binary string S of length N + M – 1 , the task is to find the minimum number of flips required to make all shortest paths from the top-left cell to the bottom-right cell equal to the given string S.
Input: mat = [[1, 0, 1, 1], [1, 1, 1, 0]], S = “10010”
Step 1: [[1, 0, 1, 1], [1, 1, 1, 0]] -> [[1, 0, 1, 1], [0, 1, 1, 0]]
Step 2: [[1, 0, 1, 1], [0, 1, 1, 0]] -> [[1, 0, 0, 1], [0, 1, 1, 0]]
Step 3: [[1, 0, 0, 1], [0, 1, 1, 0]] -> [[1, 0, 0, 1], [0, 0, 1, 0]]
Once the above steps are performed, every shortest path from the top-left to bottom-right cell are equal to S.
Therefore, the required count is 3.
Input: mat = [[1, 0, 0, 1, 0]], S = “01101”
Naive Approach: The simplest approach is to generate all possible flips in each cell of the given matrix recursively and check which combination of the minimum flips generates the matrix satisfying the required condition.
Time Complexity: O(2N * M)
Auxiliary Space: O(N * M)
Efficient Approach: To optimize the above approach, the idea is to traverse the matrix and observe that if (i, j) is the current index of the given matrix then, this position will be in the shortest path string at index (i + j) where, i ∈ [0, N-1] and j ∈ [0, M-1].
Follow the steps below to solve the problem:
- Initialize the counter as 0.
- Traverse through each position of the matrix arr.
- If the current position in the given matrix is (i, j) then, this position is in the shortest path string at (i + j)thindex.
- At each position, compare arr[i][j] and S[i + j]. If found to be equal, continue to the next position. Otherwise, increase the count by 1.
- Once the above steps are performed for the entire matrix, print the value of count as the minimum flips required.
Below is the implementation of the above approach:
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
- Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal
- Minimum substring flips required to convert given binary string to another
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum number of flips with rotation to make binary string alternating
- Number of flips to make binary string alternate | Set 1
- Sum of all pair shortest paths in a Tree
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Min flips of continuous characters to make all characters same in a string
- Minimum flips to make all 1s in left and 0s in right | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.