Skip to the code
cf.
+25 XP

#163 Find a Tile in a Sprite Sheet

medium

A game editor stores tile IDs of a sprite sheet in a grid sheet. Each row is sorted ascending, and the first ID of each row is bigger than the last ID of the row above, so reading the rows one after another gives one sorted list.

Write inSheet(sheet, target) that returns true if target is one of the IDs and false otherwise.

Use O(log(rows × cols)) time.

Example 1

Input: sheet = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 16
Output: true

16 is in the middle row.

Example 2

Input: sheet = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false

13 would sit between 11 and 16, but it is not there.

Constraints

  • 1 <= rows, cols <= 300
  • All IDs are different.
  • -10^6 <= sheet[r][c], target <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).