forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0074-search-a-2d-matrix.js
123 lines (103 loc) · 2.88 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
114
115
116
117
118
119
120
121
122
123
/**
* 74. Search a 2D Matrix
* https://leetcode.com/problems/search-a-2d-matrix/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
return matrix
.filter(row => row[0] <= target && row[row.length - 1] >= target)
.find(row => row.includes(target)) !== undefined;
};
// var searchMatrix = function(matrix, target) {
// const ROWS = matrix.length;
// const COLS = matrix[0].length;
// let top = 0;
// let bot = ROWS - 1;
// while (top <= bot) {
// const row = Math.floor((top + bot) / 2);
// if (target > matrix[row][COLS - 1]) {
// top = row + 1;
// } else if (target < matrix[row][0]) {
// bot = row - 1;
// } else {
// break;
// }
// }
// if (!(top <= bot)) {
// return false;
// }
// const row = Math.floor((top + bot) / 2);
// let l = 0;
// let r = COLS - 1;
// while (l <= r) {
// const m = Math.floor((l + r) / 2);
// if (target > matrix[row][m]) {
// l = m + 1;
// } else if (target < matrix[row][m]) {
// r = m - 1;
// } else {
// return true;
// }
// }
// return false;
// };
// const searchMatrix = function (matrix, target) {
// const rowIndex = findRow();
// if (rowIndex === true) {
// return true;
// }
// if (rowIndex < 0) {
// return false;
// }
// return findNumInRow(matrix[rowIndex]);
// function findRow() {
// let start = 0;
// let end = matrix.length - 1;
// while (start <= end) {
// const middle = Math.floor((start + end) / 2);
// const potentialRow = matrix[middle];
// const firstNumInRow = potentialRow[0];
// const lastNumInRow = potentialRow[potentialRow.length - 1];
// if (firstNumInRow === target || lastNumInRow === target) {
// return true;
// }
// if (firstNumInRow < target && lastNumInRow > target) {
// return middle;
// }
// if (target > lastNumInRow) {
// start = middle + 1;
// } else {
// end = middle - 1;
// }
// }
// return -1;
// }
// function findNumInRow(row) {
// let start = 0;
// let end = row.length - 1;
// while (start <= end) {
// const middle = Math.floor((start + end) / 2);
// const potentialResult = row[middle];
// if (potentialResult === target) {
// return true;
// }
// if (target > potentialResult) {
// start = middle + 1;
// } else {
// end = middle - 1;
// }
// }
// return false;
// }
// };