码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-06 15:14:03      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:ima   function   fun   else   integer   class   HERE   mat   +=   

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

标签:ima   function   fun   else   integer   class   HERE   mat   +=   

原文地址:https://www.cnblogs.com/Answer1215/p/10483005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!