notes/OJ notes/pages/Leetcode Search-a-2D-Matrix.md

109 lines
2.1 KiB
Markdown
Raw Normal View History

2022-06-14 23:33:35 +08:00
# Leetcode Search-a-2D-Matrix
#### 2022-06-13 15:33
---
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Algorithms:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
#algorithm #binary_search
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Data structures:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
#DS #vector #vector_2d
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Difficulty:
2022-09-03 15:41:36 +08:00
#leetcode #coding_problem #difficulty-easy
2022-06-14 23:33:35 +08:00
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
- [Link to problem](https://leetcode.com/problems/search-a-2d-matrix/submissions/)
2022-09-03 15:41:36 +08:00
---
2022-06-14 23:33:35 +08:00
### Problem
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
2022-09-03 15:41:36 +08:00
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
2022-06-14 23:33:35 +08:00
#### Examples
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
**Example 1:**
![exapmle1](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
```markdown
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
**Output:** true
```
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
**Example 2:**
![exapmle2](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
```matrix
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
**Output:** false
```
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
#### Constraints
2022-09-03 15:41:36 +08:00
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 100
- -104 <= matrix[i][j], target <= 104
2022-06-14 23:33:35 +08:00
### Thoughts
Binary search algorithm, with simple 2-d to 1-d conversion
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
> [!tip] Binary search while loops
> In binary search, remember to check for:
2022-09-03 15:41:36 +08:00
>
2022-06-14 23:33:35 +08:00
> - l <= r
2022-09-03 15:41:36 +08:00
> And use:
2022-06-14 23:33:35 +08:00
> - l = mid + 1
> - r = mid - 1
2022-09-03 15:41:36 +08:00
> Init r as m \* n - 1 to avoid mid being OOB.
2022-06-14 23:33:35 +08:00
### Solution
==note how I calculated the mid, i, j, and how I changed r and l==
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
```cpp
class Solution {
public:
bool searchMatrix(vector<vector<int>> &matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int l = 0;
2022-06-30 09:52:25 +08:00
// set r to len - 1 to avoid mid being bigger than len.
2022-06-14 23:33:35 +08:00
int r = m * n - 1;
int i; // the key to speed is to precaculate i and j, to save time.
int j;
int mid;
do {
mid = l + (r - l) / 2;
i = mid / n;
j = mid % n;
if (target == matrix[i][j]) {
return true;
} else if (target < matrix[i][j]) {
r = mid - 1;
} else {
l = mid + 1;
}
} while (l <= r);
return false;
}
};
2022-09-03 15:41:36 +08:00
```