Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 579 Bytes

1351.Count-Negative-Numbers-in-a-Sorted-Matrix.md

File metadata and controls

31 lines (23 loc) · 579 Bytes

1351. Count Negative Numbers in a Sorted Matrix

Difficulty: Easy

URL

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

Solution

Approach 1:

The code is shown below:

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