I am solving interview questions from here.
Problem : Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.
Note: No extra memory is allowed. For example:
Matrix= [1, 3, 5]
[2, 6, 9]
[3, 6, 9]
A = [1, 2, 3, 3, 5, 6, 6, 9, 9]
Median is 5. So, output is 5
.
This is my approach:
def find_median( A):
"""Returns the median value from given list"""
list_new = []
for i in range(1,len(A:
for j in i)):
list_newA[0].appendextend(jA[i])
return (sorted(list_newA[0])).pop(len(list_newA[0])/2)
Test Cases:
assert find_median([[1,3,5],[2,5,9],[3,6,11]]) == 5
assert find_median([[0,1,1],[2,6,10],[3,5,9]]) == 3
assert find_median([[1,3,4,12,14],[1,6,9,10,15],[0,1,3,3,4]]) == 4
I am able to solve the problem but I wanted to know is there a better approach to solve this problem?
I am solving interview questions from here.
Problem : Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.
Note: No extra memory is allowed. For example:
Matrix= [1, 3, 5]
[2, 6, 9]
[3, 6, 9]
A = [1, 2, 3, 3, 5, 6, 6, 9, 9]
Median is 5. So, output is 5
.
This is my approach:
def find_median( A):
"""Returns the median value from given list"""
list_new = []
for i in A:
for j in i:
list_new.append(j)
return (sorted(list_new)).pop(len(list_new)/2)
Test Cases:
assert find_median([[1,3,5],[2,5,9],[3,6,11]]) == 5
assert find_median([[0,1,1],[2,6,10],[3,5,9]]) == 3
assert find_median([[1,3,4,12,14],[1,6,9,10,15],[0,1,3,3,4]]) == 4
I am able to solve the problem but I wanted to know is there a better approach to solve this problem?
I am solving interview questions from here.
Problem : Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.
Note: No extra memory is allowed. For example:
Matrix= [1, 3, 5]
[2, 6, 9]
[3, 6, 9]
A = [1, 2, 3, 3, 5, 6, 6, 9, 9]
Median is 5. So, output is 5
.
This is my approach:
def find_median( A):
"""Returns the median value from given list"""
for i in range(1,len(A)):
A[0].extend(A[i])
return (sorted(A[0])).pop(len(A[0])/2)
Test Cases:
assert find_median([[1,3,5],[2,5,9],[3,6,11]]) == 5
assert find_median([[0,1,1],[2,6,10],[3,5,9]]) == 3
assert find_median([[1,3,4,12,14],[1,6,9,10,15],[0,1,3,3,4]]) == 4
I am able to solve the problem but I wanted to know is there a better approach to solve this problem?