Skip to content

Instantly share code, notes, and snippets.

@rtsoy
Created January 1, 2026 14:43
Show Gist options
  • Select an option

  • Save rtsoy/3268db23c076e26f60f56a174e5dab6c to your computer and use it in GitHub Desktop.

Select an option

Save rtsoy/3268db23c076e26f60f56a174e5dab6c to your computer and use it in GitHub Desktop.
74. Search a 2D Matrix
// https://leetcode.com/problems/search-a-2d-matrix/
//
// Time: O(log(m*n))
// Space: O(1)
//
// m = number of rows
// n = number of columns
// .................... //
func searchMatrix(matrix [][]int, target int) bool {
m, n := len(matrix), len(matrix[0])
lo, hi := 0, m*n-1
for lo <= hi {
mi := lo + (hi-lo)/2
row, col := mi/n, mi%n
candidate := matrix[row][col]
if candidate == target {
return true
}
if candidate > target {
hi = mi - 1
} else {
lo = mi + 1
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment