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

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix相关的知识,希望对你有一定的参考价值。

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

 

以上是关于[Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix的主要内容,如果未能解决你的问题,请参考以下文章

STL algorithm算法详解

3 - Two Pointers Algorithm

BZOJ 1452 Count

Bzoj1452 Count

[bzoj] 1036 Count

poj2777 Count Color