Matrix A, consisting of N rows and M columns, is given, with each cell containing the value 0 or 1. Rows are numbered from 0 to N−1 (from top to bottom). Columns are numbered from 0 to M−1 (from left to right). The values inside the matrix can be changed: you can select as many columns as you want, and in the selected column(s), every value will be flipped (from 0 to 1, or from 1 to 0).
The goal is to obtain the maximum number of rows whose contents are all the same value (that is, we count rows with all 0s and rows with all 1s).
Write a function:
class Solution { public int solution(int[][] A); }
that, given matrix A, returns the maximum number of rows containing all the same values that can be obtained after flipping the selected columns.
Examples:
1. Given matrix A with N = 3 rows and M = 4 columns:
the function should return 2. After flipping the values in column 1, the two last rows contain all equal values. Row 1 contains all 0s and row 2 contains all 1s.
2. Given matrix A with N = 4 rows and M = 4 columns:
the function should return 4. After flipping the values in two of the columns (columns 0 and 2), all the rows have the same value. Rows number 0 and 2 contain all 1s, and rows number 1 and 3 contain all 0s.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..100,000];
- N * M is not greater than 100,000.
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
var sw = new Stopwatch();
sw.Start();
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
sw.Stop();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
var sw = new Stopwatch();
sw.Start();
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
sw.Stop();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
Compilation failed: 3 error(s), 0 warnings Solution.cs(29,44): error CS1061: Type `System.Collections.Generic.Dictionary<string,int>.ValueCollection' does not contain a definition for `Max' and no extension method `Max' of type `System.Collections.Generic.Dictionary<string,int>.ValueCollection' could be found. Are you missing `System.Linq' using directive? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(39,44): error CS1061: Type `int[][]' does not contain a definition for `Select' and no extension method `Select' of type `int[][]' could be found. Are you missing `System.Linq' using directive? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(43,39): error CS0122: `System.Collections.Generic.HashSet<string>.ToArray()' is inaccessible due to its protection level /opt/lang/mono/lib/mono/4.5/System.Core.dll (Location of the symbol related to previous error)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
if (A.Length == 0 ||
A[0].Length == 0)
return 0;
A = RemoveDuplicateColumns(A);
var uniqueRows = new Dictionary<string,int>();
foreach (var current in A)
{
var key = string.Join("", current);
var invertKey = string.Join("", Invert(current));
if (uniqueRows.ContainsKey(key))
uniqueRows[key]++;
else if(uniqueRows.ContainsKey(invertKey))
uniqueRows[invertKey]++;
else uniqueRows[key] = 1;
}
var result = uniqueRows.Values.Max();
return result;
}
private static int[][] RemoveDuplicateColumns(int[][] a)
{
var uniqueCols = new HashSet<string>();
for (int i = 0; i < a[0].Length; i++)
{
var col = String.Join("",a.Select(c => c[i]));
if (!uniqueCols.Contains(col))
uniqueCols.Add(col);
}
var colArray = uniqueCols.ToArray();
var newA = new int[a.Length][];
for (int j = 0; j < a.Length; j++)
newA[j] = new int[colArray.Length];
for (int i = 0; i < colArray.Length; i++)
for (int j = 0; j < a.Length; j++)
{
newA[j][i] = int.Parse(colArray[i][j].ToString());
}
return newA;
}
public static int[] Invert(int [] a)
{
var array = new int[a.Length];
for (int i = 0; i < a.Length; i++)
array[i] = a[i] == 0 ? 1 : 0;
return array;
}
}
The solution obtained perfect score.