code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

1351 Count Negative Numbers In A Sorted Matrix

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * Given a m * n matrix grid which is sorted in non-increasing order
 * both row-wise and column-wise. Return the number of negative numbers in grid.
 * 
 * Example 1:
 * Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
 * Output: 8
 * Explanation: There are 8 negatives number in the matrix.
 */

function countNegativeNumbersInASortedMatrix(matrix) {
  let count = 0
  for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[0].length; j++) {
      const currentNum = matrix[i][j]
      if(currentNum < 0){
        count++
      }      
    }
  }
  return count
}

test('count Negative Numbers In A Sorted Matrix', () => {
  expect(countNegativeNumbersInASortedMatrix([[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]])).toEqual(8)
});

Created 2020-04-21T09:46:34+00:00 · Edit