[Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix

 

// Code goes here

function countNegative (M, n, m) {
  count = 0;
  i = 0;
  j = m - 1;
  
  while (j >=0 && i < n) {
    if (M[i][j] < 0) {
      count += (j+1)
      i += 1;
    } else {
      j -= 1;
    }
  }
  
  return count;
}

const M = [
  [-3,-2,-1,1],
  [-2,2,3,4],
  [,4,5,7,8]
]
console.log(countNegative(M, M.length, M[0].length)) // 4
原文地址:https://www.cnblogs.com/Answer1215/p/10483005.html