-
Notifications
You must be signed in to change notification settings - Fork 0
/
0074. Search a 2D Matrix.js
113 lines (93 loc) · 2.44 KB
/
0074. Search a 2D Matrix.js
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
// Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
//
// 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.
//
// Example 1:
//
// Input:
// matrix = [
// [1, 3, 5, 7],
// [10, 11, 16, 20],
// [23, 30, 34, 50]
// ]
// target = 3
// Output: true
//
// Example 2:
//
// Input:
// matrix = [
// [1, 3, 5, 7],
// [10, 11, 16, 20],
// [23, 30, 34, 50]
// ]
// target = 13
// Output: false
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
/** 1) Binary search twice, treat 2d matrix as a long array */
// Time O(log(hw))
const searchMatrix1 = (matrix, target) => {
if (matrix == null || matrix.length === 0) return false;
const h = matrix.length;
const w = matrix[0].length;
let l = 0;
let r = h * w - 1;
while (l <= r) {
const m = ~~((l + r) / 2);
const i = ~~(m / w); // row
const j = m % w; // col
if (matrix[i][j] === target) return true;
else if (matrix[i][j] < target) l = m + 1;
else r = m - 1;
}
return false;
};
/** 2) Binary search twice, locate row first, then column */
// Time O(log(h) + log(w))
const searchMatrix2 = (matrix, target) => {
if (matrix == null || matrix.length === 0) return false;
const h = matrix.length;
const w = matrix[0].length;
if (target < matrix[0][0] || target > matrix[h - 1][w - 1]) return false;
let l = 0;
let r = h - 1;
while (l <= r) {
const m = ~~((l + r) / 2);
if (matrix[m][0] === target) return true;
else if (matrix[m][0] < target) l = m + 1;
else r = m - 1;
}
const row = r;
l = 0;
r = w - 1;
while (l <= r) {
const m = ~~((l + r) / 2);
if (matrix[row][m] === target) return true;
else if (matrix[row][m] < target) l = m + 1;
else r = m - 1;
}
return false;
};
/** 3) Search from top right corner */
// Similar
// 240. Search a 2D Matrix II
//
// Time O(h + w), rule out one row or one column each time
const searchMatrix = (matrix, target) => {
if (matrix == null || matrix.length === 0) return false;
const h = matrix.length;
const w = matrix[0].length;
let row = 0;
let col = w - 1;
while (col >= 0 && row <= h - 1) {
if (target === matrix[row][col]) return true;
else if (target < matrix[row][col]) col--;
else if (target > matrix[row][col]) row++;
}
return false;
};