Leetcode 1476 Solution

This article provides solution to leetcode question 1476 (count-negative-numbers-in-a-sorted-matrix)

https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix

Solution

class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])

        i = 0
        j = n - 1

        ans = 0
        while i < len(grid):
            while j >= 0 and grid[i][j] < 0:
                j -= 1
            ans += n - 1 - j

            i += 1

        return ans