Count Negative Numbers in a Sorted Matrix
Real-world version: Freezing Cells on the Weather BoardThe statement here is our own: the same inputs, outputs and technique.
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.
def count_freezing(chart: list[list[int]]) -> int:public int countFreezing(int[][] chart)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
- Search a 2D MatrixReal-world version: Paged Offset LookupMedium
- 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