URL
Search a 2D Matrix - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202308/search_a_2d_matrix/main.fsx
let searchMatrix (matrix: int[,]) (target: int) : bool =
let rec searchMatrix' (matrix: int[,]) left right cols target =
if left > right then
false
else
let mid = left + (right - left) / 2
let v = matrix.[mid / cols, mid % cols]
if v = target then
true
elif v > target then
searchMatrix' matrix left (mid - 1) cols target
else
searchMatrix' matrix (mid + 1) right cols target
let rows, cols = Array2D.length1 matrix, Array2D.length2 matrix
searchMatrix' matrix 0 (rows * cols - 1) cols target