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:
def solution(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.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = 0
idx = 0
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0:
idx = i
break
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
idx = 0
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0:
idx = i
break
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
idx = 0
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0:
idx = i
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
idx = 0
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0:
idx = i
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 :
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount < onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount < onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount < onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount == onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount < onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount == onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount > onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount < onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount == onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
if zerocount > 0 and onecount > 0 and zerocount == onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if zerocount > 0 and onecount > 0 and zerocount == onecount:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i] == 0 or matrixcount[i] == len(matrix:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i] == 0 or matrixcount[i] == len(matrix[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0 == 0 or matrixcount[i] == len(matrix[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == 0 or matrixcount[i][1] == len(matrix[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(matrix[0]) or matrixcount[i][1] == len(matrix[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(matrix[0]) or matrixcount[i][1] == len(matrix[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
Traceback (most recent call last): File "exec.py", line 132, in <module> main() File "exec.py", line 94, in main result = solution( A ) File "/tmp/solution.py", line 17, in solution if matrixcount[i][0] == len(matrix[0]) or matrixcount[i][1] == len(matrix[0]): NameError: name 'matrix' is not defined
Traceback (most recent call last): File "exec.py", line 132, in <module> main() File "exec.py", line 94, in main result = solution( A ) File "/tmp/solution.py", line 17, in solution if matrixcount[i][0] == len(matrix[0]) or matrixcount[i][1] == len(matrix[0]): NameError: name 'matrix' is not defined
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(A[0]) or matrixcount[i][1] == len(matrix[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(A[0]) or matrixcount[i][1] == len(A[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(A[0]) or matrixcount[i][1] == len(A[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(A[0]) or matrixcount[i][1] == len(A[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxcount = countEqualRows(A)
matrixcount = []
for i in range(len(A)):
zerocount = 0
onecount = 0
for j in range(len(A[0])):
if A[i][j] == 0:
zerocount += 1
else:
onecount += 1
matrixcount.append([zerocount, onecount])
for i in range(len(A)):
if matrixcount[i][0] == len(A[0]) or matrixcount[i][1] == len(A[0]):
continue
else:
matrixcopy = [x for x in A]
flipvals(matrixcopy, i, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
matrixcopy = [x for x in A]
flipvals(matrixcopy, 0, 1)
maxcount = max(maxcount, countEqualRows(matrixcopy))
return maxcount
def flipvals(A, i, val):
for j in range(len(A[0])):
if A[i][j] == val:
for x1 in range(i, len(A)):
A[x1][j] = 1 - A[x1][j]
for x1 in range(0, i):
A[x1][j] = 1 - A[x1][j]
def countEqualRows(matrix):
count = 0
for i in range(len(matrix)):
total = 0
for j in range(len(matrix[0])):
total += matrix[i][j]
if total == 0 or total == len(matrix[0]):
count += 1
return count
The following issues have been detected: timeout errors.
Each row contains no more than one occurrence of 1, NM <= 100,000.
running time: 4.932 sec., time limit: 0.608 sec.