Similar Problems
Similar Problems not available
Minimum Domino Rotations For Equal Row - Leetcode Solution
Companies:
LeetCode: Minimum Domino Rotations For Equal Row Leetcode Solution
Difficulty: Medium
Problem Description:
Given a rows x cols matrix mat, where mat[i][j] is either 0 or 1, return the minimum number of rotations to make every row and every column have all the same elements.
In one rotation, you can rotate the top row to the right by one and rotate the bottom row to the left by one. Similarly, you can rotate the left column to the top by one and rotate the right column to the bottom by one.
Return -1 if it is impossible to achieve this goal.
Example: Input: mat = [[0,0,0],[0,1,0],[1,1,1]] Output: 2 Explanation: The first figure represents the initial state. The second figure represents the state after the first rotation. The third figure represents the state after the second rotation. Note that in each state, all rows and columns have the same elements.
Solution:
Approach:
As the problem states that we need to make all rows and columns identical, the only possible values are 0s and 1s. Now, for any element in the matrix to be same across all the rows and columns, it must either come from all the rows and columns having the same value or from all the rows or columns having the opposite value.
For example, for an element mat[i][j] to be same across all the rows, it must either be zero in all the rows or one in all the rows. Similarly, for an element mat[i][j] to be same across all the columns, it must either be zero in all the columns or one in all the columns.
Now, for any given row or column, we can calculate how many 0s and 1s are there in that row/column. If the number of 0s is equal to the number of 1s, we can either keep it as it is or flip it. If the number of 0s is not equal to the number of 1s, it is impossible to make all the rows/columns identical.
Therefore, our approach would be to iterate over all the rows and columns and check whether it is possible to make all rows/columns identical. If it is possible, we can calculate the minimum number of rotations required to make all rows/columns identical.
Algorithm:
-
Initialize two arrays "row" and "col" of size n and m respectively, where n is the number of rows and m is the number of columns.
-
Traverse all the rows of the matrix and for each row, count the number of 0s and 1s in that row. Store the counts in the "row" array.
-
Traverse all the columns of the matrix and for each column, count the number of 0s and 1s in that column. Store the counts in the "col" array.
-
Check whether it is possible to make all rows/columns identical or not. If it is not possible, return -1.
-
Calculate the minimum number of rotations required to make all the rows identical.
-
Calculate the minimum number of rotations required to make all the columns identical.
-
Return the sum of the minimum number of rotations required for rows and columns.
Code:
class Solution { public: int minDominoRotations(vector<vector<int>>& mat) { int n = mat.size(); // number of rows int m = mat[0].size(); // number of columns
vector<int> row(n,0); // count of 0s and 1s in each row
vector<int> col(m,0); // count of 0s and 1s in each column
// Step 2: count number of 0s and 1s in each row
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
row[i]+=mat[i][j];
}
}
// Step 3: count number of 0s and 1s in each column
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
col[j]+=mat[i][j];
}
}
// Step 4: check whether all rows or all columns can be identical or not
for(int i=0;i<n;i++){
if(row[i]==m-row[i] || row[i]==m-row[i]-1){
continue;
}
else{
return -1;
}
}
for(int j=0;j<m;j++){
if(col[j]==n-col[j] || col[j]==n-col[j]-1){
continue;
}
else{
return -1;
}
}
// Step 5: calculate minimum number of rotations required to make all the rows identical
int target = row[0]; // value to match in all rows
int flips = 0; // number of flips required in each row to make it identical
for(int i=0;i<n;i++){
if(row[i]==target){
continue;
}
else if(m-row[i]==target){
flips++;
}
else{
return -1; // impossible to make all rows identical
}
}
// Step 6: calculate minimum number of rotations required to make all the columns identical
target = col[0]; // value to match in all columns
flips = 0; // number of flips required in each column to make it identical
for(int j=0;j<m;j++){
if(col[j]==target){
continue;
}
else if(n-col[j]==target){
flips++;
}
else{
return -1; // impossible to make all columns identical
}
}
// Step 7: return the sum of minimum number of rotations required for rows and columns
return flips;
}
};
Complexity Analysis:
Time Complexity: The time complexity of this solution is O(n^2), where n is the number of rows/columns in the matrix. We need to traverse the matrix twice to count the number of 0s and 1s in each row and column. Also, we need to check whether it is possible to make all rows/columns identical or not. Therefore, the time complexity is O(n^2).
Space Complexity: The space complexity of this solution is O(n), where n is the number of rows/columns in the matrix. We need to store the counts of 0s and 1s in each row and column in two arrays "row" and "col" respectively. Therefore, the space complexity is O(n).
Minimum Domino Rotations For Equal Row Solution Code
1