Test cases
|
|
Solution 1: accepted 18ms
Binary search for each row.
Time: O(nlogn)
Space: O(1)
|
|
Solution 2: accepted 13ms
Seach from top-right or bottom left.
Time: O(nm)
Space: O(1)
|
|
Follow-up: if every row is straightly ascending (the first element of this row is larger than the last element in last row), the complexity could be O(logM+logN). Treate it as a 1D array, do a binary search (row = mid/column, col = mid%column).