Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
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.
Examples:
Input: mat[][] = [[1, 0, 1, 1], [1, 1, 1, 0]], S = “10010”
Output: 3
Explanation:
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”
Output: 5
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)th index.
- 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:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count the minimum // number of flips required int minFlips(vector<vector< int > >& mat, string s) { // Dimensions of matrix int N = mat.size(); int M = mat[0].size(); // Stores the count the flips int count = 0; for ( int i = 0; i < N; i++) { for ( int j = 0; j < M; j++) { // Check if element is same // or not if (mat[i][j] != s[i + j] - '0' ) { count++; } } } // Return the final count return count; } // Driver Code int main() { // Given Matrix vector<vector< int > > mat = { { 1, 0, 1 }, { 0, 1, 1 }, { 0, 0, 0 } }; // Given path as a string string s = "10001" ; // Function Call cout << minFlips(mat, s); return 0; } |
Java
// Java program for the above approach class GFG { // Function to count the minimum // number of flips required static int minFlips( int mat[][], String s) { // Dimensions of matrix int N = mat.length; int M = mat[ 0 ].length; // Stores the count the flips int count = 0 ; for ( int i = 0 ; i < N; i++) { for ( int j = 0 ; j < M; j++) { // Check if element is same // or not if (mat[i][j] != s.charAt(i + j) - '0' ) { count++; } } } // Return the final count return count; } // Driver Code public static void main(String[] args) { // Given Matrix int mat[][] = {{ 1 , 0 , 1 }, { 0 , 1 , 1 }, { 0 , 0 , 0 }}; // Given path as a string String s = "10001" ; // Function Call System.out.print(minFlips(mat, s)); } } // This code is contributed by Chitranayal |
Python3
# Python3 program for the above approach # Function to count the minimum # number of flips required def minFlips(mat, s): # Dimensions of matrix N = len (mat) M = len (mat[ 0 ]) # Stores the count the flips count = 0 for i in range (N): for j in range (M): # Check if element is same # or not if (mat[i][j] ! = ord (s[i + j]) - ord ( '0' )): count + = 1 # Return the final count return count # Driver Code # Given Matrix mat = [ [ 1 , 0 , 1 ], [ 0 , 1 , 1 ], [ 0 , 0 , 0 ] ] # Given path as a string s = "10001" # Function call print (minFlips(mat, s)) # This code is contributed by Shivam Singh |
C#
// C# program for the above approach using System; class GFG{ // Function to count the minimum // number of flips required static int minFlips( int [,]mat, String s) { // Dimensions of matrix int N = mat.GetLength(0); int M = mat.GetLength(1); // Stores the count the flips int count = 0; for ( int i = 0; i < N; i++) { for ( int j = 0; j < M; j++) { // Check if element is same // or not if (mat[i, j] != s[i + j] - '0' ) { count++; } } } // Return the readonly count return count; } // Driver Code public static void Main(String[] args) { // Given Matrix int [,]mat = { { 1, 0, 1 }, { 0, 1, 1 }, { 0, 0, 0 } }; // Given path as a string String s = "10001" ; // Function call Console.Write(minFlips(mat, s)); } } // This code is contributed by Amit Katiyar |
Javascript
<script> // javascript program for the // above approach // Function to count the minimum // number of flips required function minFlips(mat, s) { // Dimensions of matrix let N = mat.length; let M = mat[0].length; // Stores the count the flips let count = 0; for (let i = 0; i < N; i++) { for (let j = 0; j < M; j++) { // Check if element is same // or not if (mat[i][j] != s[(i + j)] - '0' ) { count++; } } } // Return the final count return count; } // Driver Code // Given Matrix let mat = [[ 1, 0, 1], [0, 1, 1], [0, 0, 0]]; // Given path as a string let s = "10001" ; // Function Call document.write(minFlips(mat, s)); // This code is contributed by trget_2. </script> |
4
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Please Login to comment...