Show problems as
Classic problem #1351EasyArrayBinary SearchMatrix

Count Negative Numbers in a Sorted Matrix

Real-world version: Freezing Cells on the Weather Board
Tracked in this browser only
Read Count Negative Numbers in a Sorted Matrix on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstCounting Occurrences · Binary Search, animated step by step

A weather board shows temperatures in whole degrees, laid out in rows and columns. chart[i][j] is the temperature in row i, column j.

The board is ordered in both directions: reading a row from left to right, the temperature never goes up, and reading a column from top to bottom, the temperature never goes up either. Every row holds the same number of cells.

Return how many cells of the board show a temperature below zero.

Examples

Example 1

Input
chart = [[9, 6, 2, -3], [7, 4, 0, -5], [2, 0, -1, -8], [0, -2, -6, -9]]
Output
7

The freezing cells are -3 in the first row, -5 in the second, -1 and -8 in the third, and -2, -6 and -9 in the fourth.

Example 2

Input
chart = [[6, 0, 0, -1], [0, 0, -2, -3]]
Output
3

The cells at zero are not below zero, so only -1, -2 and -3 are counted.

Example 3

Input
chart = [[3, 3], [3, 3]]
Output
0

No cell on this board is below zero.

Constraints

  • 1 <= chart.length <= 100
  • 1 <= chart[i].length <= 100
  • -100 <= chart[i][j] <= 100
  • Every row holds the same number of cells.

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.

Python
def count_freezing(chart: list[list[int]]) -> int:
Java
public int countFreezing(int[][] chart)