-
Notifications
You must be signed in to change notification settings - Fork 0
/
74.js
33 lines (26 loc) · 870 Bytes
/
74.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
// 74. Search a 2D Matrix
// https://leetcode.com/problems/search-a-2d-matrix/
// Submission 1: https://leetcode.com/submissions/detail/771903665/
// Submission 2: https://leetcode.com/submissions/detail/771909539/
const searchMatrix = (matrix, target) => {
// First Submission
// 93.70% less memory usage and 31.02% runtime
// for (let i = 0; i < matrix.length; i++) {
// if (matrix[i].includes (target)) return true
// }
// return false
// Second Submission
// 82.97% faster runtime and 18.55% less memory usage
for (let i = 0; i < matrix.length; i++) {
const new_set = new Set(matrix[i])
if (new_set.has(target)) return true
}
return false
}
// Test Examples
console.log(searchMatrix(
[[1,3,5,7],[10,11,16,20],[23,30,34,60]],
3)) // true
console.log(searchMatrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]],
13
)) // false