Leetcode Problem 1351. Count Negative Numbers in a Sorted Matrix

1351. Count Negative Numbers in a Sorted Matrix

Leetcode Solutions

Approach: Linear Traversal

  1. Initialize count to 0 to keep track of the total number of negative numbers.
  2. Initialize currRowNegativeIndex to the last index of a row (n - 1).
  3. Iterate over each row of the matrix.
  4. For each row, iterate from currRowNegativeIndex to the left until a non-negative number is found or the start of the row is reached.
  5. Update currRowNegativeIndex to the index of the last negative number found.
  6. Add the number of negative numbers in the current row to count.
  7. Repeat steps 4-6 for each row.
  8. Return count.
UML Thumbnail

Approach: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...