Skip to content
LC-1351 Easy LeetCode

1351. Count Negative Numbers in a Sorted Matrix

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 78% Topics: Array, Binary Search, Matrix
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(m + n)
# Space: O(1)

class Solution(object):
    def countNegatives(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        result, c = 0, len(grid[0])-1
        for row in grid:
            while c >= 0 and row[c] < 0:
                c -= 1
            result += len(grid[0])-1-c
        return result

Solution from kamyu104/LeetCode-Solutions · MIT