How to solve the big numbers matrix problem in Python
Given a two-dimensional integer matrix, return the total number of integers whose value is the largest in its row and column.
Step 1
Understanding the problem
- We need to find the maximum element in each row and see if it’s also the maximum in its column.
- Return the count for the number of times this condition is satisfied.
-
3is the maximum element in row0, but it’s not the maximum in its column-(column 1). -
6is the largest/maximum element in row1and column1. -
Similarly,
7is the largest element in row2and column2.
Therefore, since 6 and 7 are the maximum elements that satisfy the condition, we return 2.
Step 2
Finding the maximum element in the row
- Using the
forloop 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)
Step 3
Verifying the maximum element in its respective column
- In order to find the maximum element in the previous maximum element’s respective column, we will to transpose the matrix.
- Then, repeat step 2 to find the max element.
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))
- Repeat step 2 with the transposed matrix:
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)
Step 4
Count the elements that satisfy the condition
- Using the
forandifconditions, we compare the element inlist1andlist2, from steps 2 and 3, to see if they match. - If they do match, we append them to a list.
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)
Step-5:
Output:
- We return the length of list three.
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))
Complete code
m=[[1, 3, 2], [4, 6, 5], [1, 5, 7]]r=len(m)c=len(m[0])l=[]for i in range(r):max=0for 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=0for 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))