05 August 2008
行列均为非递增(降序),统计负数的总数量
二分查找
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
public int countNegatives(int[][] grid) {
int rows = grid.length, cols = grid[0].length;
int res = 0, lastNeg = cols - 1;
for (int row = 0; row < rows; row++) {
//check edge cases - if first element is < 0 - all elements in row are negative
if (grid[row][0] < 0) {
res+=cols;
continue;
}
//if last element is positive - it means there are no negative numbers in a row
if (grid[row][cols - 1] > 0)
continue;
//there is a mix of negative and positive ones, need to find the border. starting
//binary search
int l = 0, r = lastNeg;
while (l <= r) {
int m = l + (r - l)/2;
if (grid[row][m] < 0) {
r = m - 1;
} else
l = m + 1;
}
//l points to the first negative element, which means cols - l is a number of
//such elements
res += (cols - l); lastNeg = l;
}
return res;
}