Skip to content

Commit

Permalink
day 5, max of row which is min of col
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 26, 2020
1 parent 9d741e9 commit aa6d772
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
29 changes: 29 additions & 0 deletions day5_row_min_col_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists and functions

print('Question: Element which is min in its row and max in column')
matrix = [[1,2,3],
[4,5,6],
[7,8,9]]

def rowMinColMax(matrix):
rowMin,colMax = [],[]
for row in matrix:
# find and store max of each row
rowMin.append(min(row))
for i in range(len(matrix[0])):
# find and store max of each col
maxi = matrix[0][i]
# just travel the col from up to down, and update maxi
for j in range(len(matrix)):
maxi = max(maxi,matrix[j][i])
colMax.append(maxi)

#now find the overlap
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if rowMin[i] == colMax[j] == matrix[i][j]:
return matrix[i][j]
return False

print('Answer:')
print(rowMinColMax(matrix))
37 changes: 24 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists

print('Question: Maximum product of two elements in array')



li = [-7,-6,4,1,2,3,4,5,6]
li.sort()
# 5*6 or -7*-6 only those could give me the maximum
# product of the two smallest, neg*neg
first = li[0]*li[1]
last = li[-1]*li[-2]
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists and functions

print('Question: Element which is min in row and max in column')
matrix = [[1,2,3],
[4,5,6],
[7,8,9]]

def rowMinColMax(matrix):
rowMin,colMax = [],[]
for row in matrix:
rowMin.append(min(row))
for i in range(len(matrix[0])):
maxi = matrix[0][i]
for j in range(len(matrix)):
maxi = max(maxi,matrix[j][i])
colMax.append(maxi)

#now find the overlap
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if rowMin[i] == colMax[j] == matrix[i][j]:
return matrix[i][j]
return False

print('Answer:')
print(max(first,last))
print(rowMinColMax(matrix))



Expand Down

0 comments on commit aa6d772

Please sign in to comment.