Problem 323295 · easy · Level 03 Linear Management & Searching

Count Sub-Zero Readings

matrix · staircase search · counting

Hourly temperatures are laid out as an m x n grid temps that is sorted in non-increasing order both along every row (left to right) and along every column (top to bottom). Return how many readings are strictly negative.

Examples

Input:  temps = [[5, 3, 1, -1], [4, 2, -2, -3], [1, -1, -4, -5]]
Output: 6
Explanation: row 0 has one negative, row 1 has two, row 2 has three.

Input:  temps = [[3, 2], [1, 0]]
Output: 0

Constraints

  • 1 <= m, n <= 500
  • Target: O(m + n) time, O(1) extra space.

Goals

  • Use the sortedness of a grid to avoid inspecting every cell
  • Carry information from one row to the next
  • Return a count rather than positions
Starting Python…