2
* @param {number[][]} grid5
var countNegatives = function (grid) {7
const n = grid[0].length;9
// for each row we need to find location of first negative10
// all columns past that are negative11
// also for next row search stops before12
// location of current first negative because13
// all numbers below current are also lesser (negative)15
function binSearch(start, end, arr) {16
while (start <= end) {17
let mid = start + ((end - start) >> 1);18
if (arr[mid] < 0) end = mid - 1;21
return arr[end] < 0 ? end : end + 1;25
let searchTill = n - 1;26
for (let i = 0; i < m; i++) {28
// last row found 0 in first column33
let firstNegative = binSearch(0, searchTill, grid[i]);34
searchTill = firstNegative - 1;35
ans += n - firstNegative;