Skip to the code
cf.
+10 XP

#176 Count Negative Deltas

easy

A growth dashboard shows week-over-week changes in a grid grid. Each row is sorted from largest to smallest (left to right), and each column is sorted from largest to smallest (top to bottom).

Write countNegative(grid) that returns how many cells hold a negative number. Use O(rows + cols) time.

Example 1

Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8

The negative cells fill the bottom-right corner: 1 + 1 + 2 + 4 = 8.

Example 2

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

0 is not negative.

Constraints

  • 1 <= rows, cols <= 100
  • -100 <= grid[r][c] <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).