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

Each row and each column are already SORTED in the given matrix!

const mix = [[-3, -2, -1, 3], [-1, 0, 1, 3], [0, 2, 4, 5]];

/**
 * Start from top right slot, go from right to left, top to bottom
 * case 1; If the current value is larger than 0, keep moving to left
 * case 2: if the current value is smaller than , menas the rest of value should
 *  also less than zero, count = count + 1 + j
 *  then move to next row
 */
// findNegativeNumbers :: [num] -> num
function findNegativeNumbers(data) {
  let count = 0;
  let i = 0,
    j = data[0].length - 1;
  while (i <= data.length - 1 && j >= 0) {
    const current = data[i][j];
    if (current >= 0) {
      j--;
    } else {
      count += j + 1;
      i++;
    }
  }
  return count;
}

console.log(findNegativeNumbers(mix)); // 4

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

时间: 2024-10-31 06:48:58

[Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix的相关文章

flutter 页面布局 Paddiing Row Column Expanded 组件

Flutter Paddiing 组件 在 html 中常见的布局标签都有 padding 属性,但是 Flutter 中很多 Widget 是没有 padding 属 性.这个时候我们可以用 Padding 组件处理容器与子元素直接的间距. 属性 说明 padding padding 值, EdgeInsetss 设置填充的值 child 子组件 import 'package:flutter/material.dart'; void main() => runApp(MyApp()); cl

1351. Count Negative Numbers in a Sorted Matrix

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 ar

Count the number of occurrences in a sorted array

Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr

good article————K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)

这是本人在研究leetcode中Median of Two Sorted Arrays一题目的时候看到一篇文章,觉得非常好,其中对快速排序重新实现. 文章来源于http://www.geeksforgeeks.org/这个网站. We recommend to read following post as a prerequisite of this post. K'th Smallest/Largest Element in Unsorted Array | Set 1 Given an ar

xlrd doc

The xlrd Module A Python module for extracting data from MS Excel ™ spreadsheet files. Version 0.7.3 -- February/March 2012 General information Acknowledgements Development of this module would not have been possible without the document "OpenOffice.

Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it. This matrix has the following properties: Integers in each row are sorted from left to right. Integers in each column are sorted from up to bottom

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascend

【LeetCode】240. Search a 2D Matrix II

Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascendin

240. Search a 2D Matrix II

题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bott