Count Negative Numbers in a Sorted Matrix (Leetcode 1351)

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

class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
        
        m=len(grid)
        n=len(grid[0])
        
        r=m-1
        c=0
        ans=0
        while(r>=0 and c<n):
            if(grid[r][c]<0):
                ans=ans+(n-c)
                r=r-1
            else:
                c=c+1
        return ans

Last updated