2
2
id : search-a-2D-matrix
3
3
title : Search a 2D matrix
4
4
difficulty : Medium
5
- sidebar_label : 0073 -search2Dmatrix
5
+ sidebar_label : 0074 -search2Dmatrix
6
6
tags :
7
7
- Array
8
8
- Binary Search
17
17
18
18
## Problem Description
19
19
20
- You are given an m x n integer matrix matrix with the following two properties:
20
+ You are given an $m \times n$ integer matrix matrix with the following two properties:
21
21
22
22
- Each row is sorted in non-decreasing order.
23
23
- The first integer of each row is greater than the last integer of the previous row.
@@ -32,13 +32,19 @@ You must write a solution in $O(log(m * n))$ time complexity.
32
32
#### Example 1:
33
33
34
34
** Input** :
35
- Matrix = [[ 1,3,5,7] , [ 10,11,16,20] , [ 23,30,34,60]] , target = 3
35
+ ```
36
+ Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]]
37
+ Target = 3
38
+ ```
36
39
37
40
** Output** : true
38
41
39
42
#### Example 2:
40
43
** Input** :
41
- Matrix = [[ 1,3,5,7] , [ 10,11,16,20] , [ 23,30,34,60]] , target = 13
44
+ ```
45
+ Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]]
46
+ Target = 13
47
+ ```
42
48
43
49
** Output** : false
44
50
@@ -48,7 +54,7 @@ Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]], target = 13
48
54
- m == matrix.length
49
55
- n == matrix[ i] .length
50
56
- 1 <= m, n <= 100
51
- - -10^4 <= matrix[ i] [ j ] , target <= 10^4
57
+ - -10< sup >4</ sup > <= matrix[ i] [ j ] , target <= 10< sup >4</ sup >
52
58
53
59
### Approach
54
60
@@ -146,8 +152,8 @@ class Solution {
146
152
147
153
Complexity Analysis
148
154
149
- - Time Complexity: $O(log(NxM ))$, where N = given row number, M = given column number.
155
+ - Time Complexity: $O(log(N.M ))$, where N = given row number, M = given column number.
150
156
151
157
Reason: We are applying binary search on the imaginary 1D array of size NxM.
152
158
153
- - Space Complexity: $O(1)$ as we are not using any extra space.
159
+ - Space Complexity: $O(1)$ as we are not using any extra space.
0 commit comments