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
Connectez-vous pour voir l'historique des soumissions
Se connecterConnectez-vous pour utiliser le Mentor IA
Se connecterVisible tests
1
Input
[[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]Expected
82
Input
[[3,2],[1,0]]Expected
03
Input
[[-1]]Expected
14
Input
[[5,1,0]]Expected
05
Input
[[-5,-5],[-5,-5]]Expected
46
Input
[[1,-1],[-1,-1]]Expected
3Focus radio
Paused · SomaFM · Fluid