- brute force approach would be just iterating through every element in the matrix and checking if it equals our target value
- this would be O(n*m) time, which is not acceptable for the constraint of it being O(log(m*n))
- the input matrix is sorted (this helps)
- a logarithmic solution for searching for an element would be some form of binary search
🤔What Did I Struggle With?
- nailed this question in the mock, just ran out of time by like a minute due to spending too much time of the first question
💡 Explanation of Solution
We can leverage binary search twice:
1. **Binary Search on Rows:**
- Treat the matrix as a list of rows.
- Use binary search to identify the row that could potentially contain the target.
- This works because the first and last elements of each row provide a range.
1. **Binary Search on Columns (within the selected row):**
- Once the row is identified, perform binary search within that row to find the target.
- This works because each row is sorted.
⌛ Complexity Analysis
Time Complexity: O(log (n*m))
Space Complexity: O(1)