Skip to the code
cf.
+25 XP

#175 Value in a Sorted Heatmap

medium

A latency heatmap stores values in a grid grid where each row is sorted ascending from left to right and each column is sorted ascending from top to bottom. A row does not continue where the previous row ended, so the grid read row by row is not one sorted list.

Write inHeatmap(grid, target) that returns true if target is in the grid and false otherwise.

Use O(rows + cols) time.

Example 1

Input: grid = [[1,4,7,11],[2,5,8,12],[3,6,9,16],[10,13,14,17]], target = 9
Output: true

9 is at row 2, column 2.

Example 2

Input: grid = [[1,4,7,11],[2,5,8,12],[3,6,9,16],[10,13,14,17]], target = 15
Output: false

15 would fall between values but is not in the grid.

Constraints

  • 1 <= rows, cols <= 300
  • -10^9 <= grid[r][c], target <= 10^9
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).