Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
Last Updated :
08 May, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int minFlips(vector<vector< int > >& mat,
string s)
{
int N = mat.size();
int M = mat[0].size();
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (mat[i][j]
!= s[i + j] - '0' ) {
count++;
}
}
}
return count;
}
int main()
{
vector<vector< int > > mat
= { { 1, 0, 1 },
{ 0, 1, 1 },
{ 0, 0, 0 } };
string s = "10001" ;
cout << minFlips(mat, s);
return 0;
}
|
Java
class GFG {
static int minFlips( int mat[][],
String s)
{
int N = mat.length;
int M = mat[ 0 ].length;
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
if (mat[i][j] !=
s.charAt(i + j) - '0' )
{
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int mat[][] = {{ 1 , 0 , 1 },
{ 0 , 1 , 1 }, { 0 , 0 , 0 }};
String s = "10001" ;
System.out.print(minFlips(mat, s));
}
}
|
Python3
def minFlips(mat, s):
N = len (mat)
M = len (mat[ 0 ])
count = 0
for i in range (N):
for j in range (M):
if (mat[i][j] ! = ord (s[i + j]) -
ord ( '0' )):
count + = 1
return count
mat = [ [ 1 , 0 , 1 ],
[ 0 , 1 , 1 ],
[ 0 , 0 , 0 ] ]
s = "10001"
print (minFlips(mat, s))
|
C#
using System;
class GFG{
static int minFlips( int [,]mat,
String s)
{
int N = mat.GetLength(0);
int M = mat.GetLength(1);
int count = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
if (mat[i, j] !=
s[i + j] - '0' )
{
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int [,]mat = { { 1, 0, 1 },
{ 0, 1, 1 },
{ 0, 0, 0 } };
String s = "10001" ;
Console.Write(minFlips(mat, s));
}
}
|
Javascript
<script>
function minFlips(mat, s)
{
let N = mat.length;
let M = mat[0].length;
let count = 0;
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++)
{
if (mat[i][j] !=
s[(i + j)] - '0' )
{
count++;
}
}
}
return count;
}
let mat = [[ 1, 0, 1],
[0, 1, 1], [0, 0, 0]];
let s = "10001" ;
document.write(minFlips(mat, s));
</script>
|
Time Complexity: O(N * M), The time complexity of the given C++ program is O(N*M), where N and M are the number of rows and columns in the input matrix, respectively. This is because the program uses two nested loops to iterate over all the elements in the matrix, and the time taken to execute each iteration is constant.
Auxiliary Space: O(N * M), The space complexity of the program is also O(N*M), as it stores the input matrix as a vector of vectors, which requires NM elements to store all the matrix elements. Additionally, it stores the input string as a separate string variable, which requires O(S) space, where S is the length of the string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...