Negatives in Sorted Grid

When a table is already sorted, you can count negative values smarter than by checking every cell one by one. Given a matrix grid, where values decrease from left to right in each row and from top to bottom in each column, count how many numbers are negative. Return that count.

Примеры

Ввод
[[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]]
Вывод
8
Ввод
[[3, 2], [1, 0]]
Вывод
0
Ввод
[[-1]]
Вывод
1

Нужно реализовать

def count_negatives(grid: list[list[int]]) -> int:

Теги

arraybinary-searchtwo-pointers
PostgreSQLv16

Här visas resultatet av din kod

Visible tests
1
Input
[[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Expected8
2
Input
[[3,2],[1,0]]
Expected0
3
Input
[[-1]]
Expected1
4
Input
[[5,1,0]]
Expected0
5
Input
[[-5,-5],[-5,-5]]
Expected4
6
Input
[[1,-1],[-1,-1]]
Expected3
Focus radio
Paused · SomaFM · Fluid