Searching a 2D matrix means deciding whether a target value appears anywhere in a grid of numbers — and here the grid has a property that makes the search fast. This is LeetCode 240, Search a 2D Matrix II: you are given a matrix whose every row is sorted ascending left-to-right and whose every column is sorted ascending top-to-bottom, and you return whether target is somewhere inside. Those two sort orders are the whole point — used together they let you rule out an entire row or an entire column with a single comparison, so you never have to scan the whole grid.
search2dMatrix(matrix, target)
// matrix number[][] — every row ascends left-to-right, every column ascends top-to-bottom
// target number — the value to look for
// returns boolean — true if target appears in matrix, otherwise false
const matrix = [
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30],
];
search2dMatrix(matrix, 5); // true — it sits at row 1, column 1
search2dMatrix(matrix, 20); // false — 20 is within range but not in the grid
search2dMatrix([[42]], 42); // true — single cell, matches
search2dMatrix([], 7); // false — empty matrix, nothing to find
search2dMatrix([[]], 7); // false — one row with zero columns
10 starts row 3 but 15 ends row 0), so you cannot treat the grid as one long sorted list.true or false; you do not need the coordinates.[] or a matrix whose rows are empty [[]] holds no values at all.false.m rows and n columns, a single staircase walk beats both scanning all m × n cells and binary-searching every row.