-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOUNT -VE N0. IN A SORTED MATRIX.cpp
57 lines (46 loc) · 1.15 KB
/
COUNT -VE N0. IN A SORTED MATRIX.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
int bs(vector<int> v, int col)
{
int s = 0;
int e = col - 1;
while (s <= e)
{
int mid = (s + e) / 2;
if (v[mid] < 0)
e = mid - 1;
else
s = mid + 1;
}
if (s >= col)
return 0;
return col - s;
}
int countNegatives(vector<vector<int>> &grid)
{
int r = grid.size();
int c = grid[0].size();
int i, j;
int count = 0;
for (int i = 0; i < r; i++)
{
count = count + bs(grid[i], c);
}
return count;
// BRUTE FORCE
// int count = 0;
// for(int i =0;i<grid.size();i++)
// {
// for(int j =0;j<grid[i].size();j++)
// {
// if(grid[i][j] <0)
// count++;
// }
// }
// return count;
}
};
// leetcode problem - 1331