Skip to content
Decoder

Count Negative Numbers in a Sorted Matrix

Easy

Problem

You are given an m x n grid grid that is sorted in non-increasing order both along each row and down each column. Return the number of negative values in the grid. Can you do it in O(m + n)?

Example 1

grid = [[4, 3, 2, -1],

Example 2

        [3, 2, 1, -1],

Example 3

        [1, 1, -1, -2],

Example 4

Input
[-1, -1, -2, -3]]
Output
8

Example 5

Input
grid = [[3, 2], [1, 0]]
Output
0

Constraints

  • 1 <= m, n <= 100
  • -100 <= grid[i][j] <= 100

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.