Given a two-dimensional integer matrix, return the total number of integers whose value is the largest in its row and column.
3
is the maximum element in row 0
, but it’s not the maximum in its column-(column 1).
6
is the largest/maximum element in row 1
and column 1
.
Similarly, 7
is the largest element in row 2
and column 2
.
Therefore, since 6
and 7
are the maximum elements that satisfy the condition, we return 2
.
for
loop we find the maximum element in each row and append each element into a list.Ex:
|1||2|
|4||5|
|1|5||
Here, 3, 6, and 7 are going to be appended to list1
.
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]]
r=len(m)
c=len(m[0])
l=[]
for i in range(r):
max=0
for j in range(c):
if m[i][j] > max:
max = m[i][j]
l.append(max)
Ex: transpose method
| 1 | 4 | 1 |
| 3 | 6 | 5 |
| 2 | 5 | 7 |
matrix2 = (Transpose of matrix1)
We can take transpose using the zip method.
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]]
m2 = list(zip(*m))
Ex:
| 1 | | 1 |
| 3 | | 5 |
| 2 | 5 | |
Here, 4, 6, and 7 will be appended to list2
:
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]]
r=len(m)
c=len(m[0])
m2 = list(zip(*m))
l2=[]
for i in range(r):
max=0
for j in range(c):
if m[i][j] > max:
max = m[i][j]
l2.append(max)
for
and if
conditions, we compare the element in list1
and list2
, from steps 2 and 3, to see if they match.EX:
LIST1=[3,6,7]
LIST2=[4,6,7]
Therefore, since 6 and 7 are the same, we append those two elements to list3
:
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]]
r=len(m)
c=len(m[0])
l=[]
for i in range(r):
max=0
for j in range(c):
if m[i][j] > max:
max = m[i][j]
l.append(max)
m2 = list(zip(*m))
l2=[]
for i in range(r):
max=0
for j in range(c):
if m[i][j] > max:
max = m[i][j]
l2.append(max)
a=[]
for r in range(len(m)):
for c in range(len(m2)):
b = m[r][c]
if (l[r], l2[c]) == (b, b):
a.append(b)
EX:
List3 = [6,7]
Therefore, the length is 2
.
Hence, the output is 2
.
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]]
r=len(m)
c=len(m[0])
l=[]
for i in range(r):
max=0
for j in range(c):
if m[i][j] > max:
max = m[i][j]
l.append(max)
m2 = list(zip(*m))
l2=[]
for i in range(r):
max=0
for j in range(c):
if m[i][j] > max:
max = m[i][j]
l2.append(max)
a=[]
for r in range(len(m)):
for c in range(len(m2)):
b = m[r][c]
if (l[r], l2[c]) == (b, b):
a.append(b)
print(len(a))
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]] r=len(m) c=len(m[0]) l=[] for i in range(r): max=0 for j in range(c): if m[i][j] > max: max = m[i][j] l.append(max) m2 = list(zip(*m)) l2=[] for i in range(r): max=0 for j in range(c): if m[i][j] > max: max = m[i][j] l2.append(max) a=[] for r in range(len(m)): for c in range(len(m2)): b = m[r][c] if (l[r], l2[c]) == (b, b): a.append(b) print(len(a))
RELATED TAGS
CONTRIBUTOR
View all Courses