Count Negative Numbers in a Sorted Matrix

Problem Id: 1351 Difficulty: Easy Tag: Array Tag: Binary Search


Intuition

Since there are at most 10,000 elements in the grid, we could directly solve this problem by brute force.

Solution


class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
        count = 0
        for row in grid:
            for elem in row:
                if elem < 0:
                    count += 1
        return count