-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.kt
More file actions
33 lines (29 loc) · 847 Bytes
/
Solution.kt
File metadata and controls
33 lines (29 loc) · 847 Bytes
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
package problems.searchA2DMatrix
import readmeGeneration.ProblemDifficulty
import readmeGeneration.ProblemSolution
@ProblemSolution(
74,
"Search a 2D Matrix",
ProblemDifficulty.MEDIUM,
"https://leetcode.com/problems/search-a-2d-matrix/"
)
object Solution {
fun searchMatrix(matrix: Array<IntArray>, target: Int): Boolean {
val arrSize = matrix[0].size
var low = 0
var high = matrix.size * arrSize - 1
while (low <= high) {
// 0 1 2
// 3 4 5
val pivot = low + (high - low) / 2
val elem = matrix[pivot / arrSize][pivot % arrSize]
if (elem == target) return true
if (target < elem) {
high = pivot - 1
} else {
low = pivot + 1
}
}
return false
}
}