给定一个维度为N * M的二进制矩阵mat[][] ,任务是通过选择矩阵的任何列并在每个操作中翻转该列的所有元素来最大化仅由相等元素组成的行数。打印可以构成相等元素的最大行数。
例子:
Input: mat[][] = { { 0, 1, 0, 0, 1 }, { 1, 1, 0, 1, 1 }, { 1, 0, 1, 1, 0 } }
Output: 2
Explanation:
Select the 2nd column and flip all the elements of that column to modify mat[][] to { { 0, 0, 0, 0, 1 }, { 1, 0, 0, 1, 1 }, { 1, 1, 1, 1, 0 } }
Select the 5th column and flip all the elements of that column to modify mat[][] to { { 0, 0, 0, 0, 0 }, { 1, 0, 0, 1, 0 }, { 1, 1, 1, 1, 1 } }
Since all elements of the 1st row and the 3rd row of the matrix are equal and is also the maximum number of rows that can be made to contain equal elements only, the required output is 2.
Input: mat[][] = { {0, 0}, {0, 1} }
Output: 1
朴素的方法:解决这个问题的最简单的方法是计算仅包含相等元素的行数,对于选择列组合并翻转其元素的每种可能方法。最后,打印为上述任何组合获得的最大计数。
时间复杂度: O(N * M * 2 M )
辅助空间: O(N * M)
高效的方法:为了优化上述方法,这个想法是基于这样一个事实,如果一行是另一行的1的补码或两行相同,那么只有两行才会包含相等的元素,只有通过执行给定的操作。
Illustration:
Let us consider the following matrix:
1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 |
0 | 1 | 0 | 1 | 0 | 0 | 1 | 1 |
0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 0 | 0 | 1 | 1 |
1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 |
In the above matrix, 1st and 2nd rows are 1’s complement of each other, and the 5th and 4th rows are 1’s complement of each other.
请按照以下步骤解决问题:
- 初始化一个变量,比如cntMaxRows ,以存储仅由相等元素组成的行的最大计数。
- 初始化一个 Map,比如mp ,以存储矩阵的所有可能行。
- 遍历矩阵的每一行并将其存储在Map 中。
- 使用变量row遍历矩阵的每一行。计算1的行和更新cntMaxRows =的补体最大值(cntMaxRows,熔点[行] +熔点[1’s_comp_row])
- 最后,打印cntMaxRows的值。
下面是我们方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the maximum number
// of rows containing all equal elements
int maxEqrows(vector >& mat,
int N, int M)
{
// Store each row of the matrix
map, int> mp;
// Traverse each row of the matrix
for (int i = 0; i < N; i++) {
// Update frequency of
// current row
mp[mat[i]]++;
}
// Stores maximum count of rows
// containing all equal elements
int cntMaxRows = 0;
// Traverse each row of the matrix
for (int i = 0; i < N; i++) {
// Stores 1's complement
// of the current row
vector onesCompRow(M, 0);
// Traverse current row
// of the given matrix
for (int j = 0; j < M; j++) {
// Stores 1s complement of
// mat[i][j]
onesCompRow[j]
= (mat[i][j] ^ 1);
}
// Update cntMaxRows
cntMaxRows = max(cntMaxRows,
mp[mat[i]] + mp[onesCompRow]);
}
return cntMaxRows;
}
// Driver Code
int main()
{
vector > mat
= { { 0, 1, 0, 0, 1 },
{ 1, 1, 0, 1, 1 },
{ 1, 0, 1, 1, 0 } };
// Stores count of rows
int N = mat.size();
// Stores count of columns
int M = mat[0].size();
cout << maxEqrows(mat, N, M);
}
Java
// Java program to implement
import java.util.*;
class GFG
{
// Function to find the maximum number
// of rows containing all equal elements
static int maxEqrows(Vector> mat,
int N, int M)
{
// Store each row of the matrix
HashMap, Integer> mp = new HashMap<>();
// Traverse each row of the matrix
for (int i = 0; i < N; i++)
{
// Update frequency of
// current row
if(mp.containsKey(mat.get(i)))
{
mp.put(mat.get(i), mp.get(mat.get(i)) + 1);
}
else
{
mp.put(mat.get(i), 1);
}
}
// Stores maximum count of rows
// containing all equal elements
int cntMaxRows = 0;
// Traverse each row of the matrix
for (int i = 0; i < N; i++)
{
// Stores 1's complement
// of the current row
Vector onesCompRow = new Vector();
for(int j = 0; j < M; j++)
{
onesCompRow.add(0);
}
// Traverse current row
// of the given matrix
for (int j = 0; j < M; j++)
{
// Stores 1s complement of
// mat[i][j]
onesCompRow.set(j, mat.get(i).get(j) ^ 1);
}
// Update cntMaxRows
if(!mp.containsKey(mat.get(i)))
{
cntMaxRows = Math.max(cntMaxRows, mp.get(onesCompRow));
}
else if(!mp.containsKey(onesCompRow))
{
cntMaxRows = Math.max(cntMaxRows, mp.get(mat.get(i)));
}
else
{
cntMaxRows = Math.max(cntMaxRows, mp.get(mat.get(i)) +
mp.get(onesCompRow));
}
}
return cntMaxRows;
}
// Driver code
public static void main(String[] args)
{
Vector> mat = new Vector>();
mat.add(new Vector());
mat.add(new Vector());
mat.add(new Vector());
mat.get(0).add(0);
mat.get(0).add(1);
mat.get(0).add(0);
mat.get(0).add(0);
mat.get(0).add(1);
mat.get(1).add(1);
mat.get(1).add(1);
mat.get(1).add(0);
mat.get(1).add(1);
mat.get(1).add(1);
mat.get(2).add(1);
mat.get(2).add(0);
mat.get(2).add(1);
mat.get(2).add(1);
mat.get(2).add(0);
// Stores count of rows
int N = mat.size();
// Stores count of columns
int M = mat.get(0).size();
System.out.println(maxEqrows(mat, N, M));
}
}
// This code is contributed by divyesh072019
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the maximum number
// of rows containing all equal elements
static int maxEqrows(List> mat, int N, int M)
{
// Store each row of the matrix
Dictionary, int> mp = new Dictionary, int>();
// Traverse each row of the matrix
for (int i = 0; i < N; i++) {
// Update frequency of
// current row
if(mp.ContainsKey(mat[i]))
{
mp[mat[i]]++;
}
else{
mp[mat[i]] = 1;
}
}
// Stores maximum count of rows
// containing all equal elements
int cntMaxRows = 0;
// Traverse each row of the matrix
for (int i = 0; i < N; i++) {
// Stores 1's complement
// of the current row
List onesCompRow = new List();
for(int j = 0; j < M; j++)
{
onesCompRow.Add(0);
}
// Traverse current row
// of the given matrix
for (int j = 0; j < M; j++) {
// Stores 1s complement of
// mat[i][j]
onesCompRow[j] = (mat[i][j] ^ 1);
}
// Update cntMaxRows
if(!mp.ContainsKey(mat[i]))
{
cntMaxRows = Math.Max(cntMaxRows, mp[onesCompRow] + 1);
}
else if(!mp.ContainsKey(onesCompRow))
{
cntMaxRows = Math.Max(cntMaxRows, mp[mat[i]] + 1);
}
else{
cntMaxRows = Math.Max(cntMaxRows, mp[mat[i]] + mp[onesCompRow] + 1);
}
}
return cntMaxRows;
}
// Driver code
static void Main() {
List> mat = new List>();
mat.Add(new List { 0, 1, 0, 0, 1 });
mat.Add(new List { 1, 1, 0, 1, 1 });
mat.Add(new List { 1, 0, 1, 1, 0 });
// Stores count of rows
int N = mat.Count;
// Stores count of columns
int M = mat[0].Count;
Console.WriteLine(maxEqrows(mat, N, M));
}
}
// This code is contributed by divyeshrabadiya07
2
时间复杂度: O(N * M)
辅助空间: O(M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live