-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path240.SearchA2DMatrixIi.cpp
118 lines (111 loc) · 2.96 KB
/
240.SearchA2DMatrixIi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* @lc app=leetcode id=240 lang=cpp
*
* [240] Search a 2D Matrix II
*
* https://leetcode.com/problems/search-a-2d-matrix-ii/description/
*
* algorithms
* Medium (44.45%)
* Likes: 4558
* Dislikes: 87
* Total Accepted: 437.8K
* Total Submissions: 966.5K
* Testcase Example:
* '[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\n5'
*
* Write an efficient algorithm that searches for a target value in an m x n
* integer matrix. The matrix has the following properties:
*
*
* Integers in each row are sorted in ascending from left to right.
* Integers in each column are sorted in ascending from top to bottom.
*
*
*
* Example 1:
*
*
* Input: matrix =
* [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],
* target = 5
* Output: true
*
*
* Example 2:
*
*
* Input: matrix =
* [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],
* target = 20
* Output: false
*
*
*
* Constraints:
*
*
* m == matrix.length
* n == matrix[i].length
* 1 <= n, m <= 300
* -10^9 <= matix[i][j] <= 10^9
* All the integers in each row are sorted in ascending order.
* All the integers in each column are sorted in ascending order.
* -10^9 <= target <= 10^9
*
*
*/
// @lc code=start
#include <vector>
class Solution {
public:
bool searchMatrix(std::vector<std::vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
std::pair<int, int> valid_range{0, m - 1};
int left = 0;
int right = m - 1;
// filter out rows where all elements are larger than target
while (left < right) {
int mid = (left + right) / 2 + 1;
if (matrix[mid][0] > target) {
right = mid - 1;
valid_range.second = right;
} else if (matrix[mid][0] < target) {
left = mid;
} else {
return true;
}
}
// filter out rows where all elements are smaller than target
left = 0;
while (left < right) {
int mid = (left + right) / 2;
if (matrix[mid][n - 1] < target) {
left = mid + 1;
valid_range.first = left;
} else if (matrix[mid][n - 1] > target) {
right = mid;
} else {
return true;
}
}
// for the rest rows, do a binary search for each row
for (int r = valid_range.first; r <= valid_range.second; ++r) {
left = 0;
right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (matrix[r][mid] < target) {
left = mid + 1;
} else if (matrix[r][mid] > target) {
right = mid - 1;
} else {
return true;
}
}
}
return false;
}
};
// @lc code=end