I have the following problem
Assume that we have a 9*8 matrix
A matrix is said to have a “saddle point”, if in some position
is the smallest value in its row and the largest value in its column .
In symbols , a[i][j] is a saddle point if
a[i][j]=min a[i][k] ==max a[k][k]
1<=k<=8 1<=k<=9
Please help me finding the computer location of the saddle point.
Given MxN matrix, this is
O(MN), which is optimal.Why is this optimal?
Because there are MxN values, and they each need to be looked at, so for your answer to be certain (i.e. not probabilistic), the lowerbound is
O(MN).Can this be optimized further?
You can optimize this a bit. It’ll still be
O(MN), but instead of finding the maximum/minimum values, you find their indices instead. This can make the second phaseO(M)in the best case (i.e. when there’s a unique min/max in a row/column).Note that in the worst case, there are
O(MN)saddle points: that’s when the numbers in the array are all equal.