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.
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if (row_len==1) or len(A)=1
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1:
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that doesnt need calculations
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that doesnt need calculation
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that doesnt need calculation
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that dont need calculation
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that don't need calculation.
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that don't need calculation.
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
#creating inverted string cause inverted strings after changes would fit to condition as well (n alwasy given to
#save time.
def invert (array, n):
result=[]
for x in range(0,n):
if array[x]==0 : result.append(1)
else: result.append(0)
return (result)
def solution(A):
row_len=len(A[0]) #len of row.
if row_len==1 or len(A)==1: return (len(A)) #expections that don't need calculation.
score=0
A.sort()
while (len(A)>score): #continue till amounts of rows in matrix is bigger then current biggest score.
current=A[0]
current_inverted=invert(current,row_len)
current_score=0
x=0
while (x<len(A)): #searching for string and inverted string and removing them from matrix.
if A[x]==current or A[x]==current_inverted:
current_score +=1
A.pop(x)
else: x+=1
score=max(score, current_score)
return(score)
The solution obtained perfect score.