aboutsummaryrefslogtreecommitdiff
path: root/count_negative_num_matrix.cpp
blob: 2496cf89ad5ef203520814f325363b10309c5c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 1351. Count Negative Numbers in a Sorted Matrix
#include "leetcode.h"

// given an 'm x n' matrix 'grid' which is sorted in non-increasing order both
// row-wise and column-wise, return the number of negative numbers in 'grid'

class Solution {
public:
  int countNegatives(vvd(int) & grid) {
    int ans = 0;
    for (vector<int> row : grid)
      for (int i : row)
        if (i < 0)
          ans++;
    return ans;
    for (vector<int> row : grid)
      ans += upper_bound(row.rbegin(), row.rend(), -1) - row.rbegin();
    return ans;
    int m = grid.size(), n = grid[0].size(), r = 0, c = n - 1;
    while (r < m) {
      while (c >= 0 && grid[r][c] < 0)
        c--;
      ans += n - c - 1;
      r++;
    }
    return ans;
  }
};

int main() {
  Solution obj;
  vvd(int)
      g1 = {{4, 3, 2, -1}, {3, 2, 1, -1}, {1, 1, -1, -2}, {-1, -1, -2, -3}};
  vvd(int) g2 = {{3, 2}, {1, 0}};
  printf("%d\n", obj.countNegatives(g1)); // expect: 8
  printf("%d\n", obj.countNegatives(g2)); // expect: 0
}