Search a 2D Matrix
Real-world version: Paged Offset LookupThe statement here is our own: the same inputs, outputs and technique.
A calibration archive keeps signed offset readings in fixed-width pages. You are given pages, a list of m pages of n readings each, laid out so that:
- readings grow strictly from left to right inside a page, and
- every reading on a page is strictly smaller than every reading on the page after it.
Given an integer probe, report whether that exact reading is filed anywhere in the archive.
Return true when the archive holds probe, otherwise false. The archive can be large, so your lookup must run in time logarithmic in the total reading count m * n; walking every page is too slow.
Examples
Example 1
- Input
- pages = [[-9, -4, 1], [6, 11, 20]], probe = 11
- Output
- true
Reading 11 sits in the second slot of the second page, so the archive holds it.
Example 2
- Input
- pages = [[-9, -4, 1], [6, 11, 20]], probe = 5
- Output
- false
Nothing between 1 and 6 was ever filed, so 5 is absent even though it falls inside the archive's range.
Example 3
- Input
- pages = [[-10], [0], [10]], probe = 10
- Output
- true
Pages of a single reading are allowed; the last page carries 10.
Constraints
- m == pages.length
- n == pages[i].length
- 1 <= m, n <= 100
- -10^4 <= pages[i][j], probe <= 10^4
- Readings ascend across the whole archive when the pages are read in order
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def locate_offset(pages: list[list[int]], probe: int) -> bool:public boolean locateOffset(int[][] pages, int probe)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Count Negative Numbers in a Sorted MatrixReal-world version: Freezing Cells on the Weather BoardEasy
- Kth Smallest Element in a Sorted MatrixReal-world version: Kth Smallest Drift ReadingMedium
- Path With Minimum EffortReal-world version: Route With the Gentlest ClimbMedium
- Find a Peak Element IIReal-world version: Any Summit in a Height FieldMedium