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 are 8 negatives number in the matrix.

Example 2:

Input: grid = [[3,2],[1,0]]
Output: 0

Example 3:

Input: grid = [[1,-1],[-1,-1]]
Output: 3

Example 4:

Input: grid = [[-1]]
Output: 1

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 100
  • -100 <= grid[i][j] <= 100
class Solution {
    public int countNegatives(int[][] grid) {
        int res = 0;
        for(int i = 0; i < grid.length; i++){
            for(int j = 0; j < grid[0].length; j++){
                if(grid[i][j] < 0) res++;
            }
        }
        return res;
    }
}

我没看错吧?brute force还66.9%

class Solution {
    public int countNegatives(int[][] grid) {
        int res = 0;
        int m = grid.length;
        int n = grid[0].length;
        int i = 0;
        int j = n - 1;
        while(i < m && j >= 0){
            if(grid[i][j] < 0){
                res += m - i;
                j--;
            }
            else i++;
        }
        return res;
    }
}

O(M + N),从右上角开始扫到左下角

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12325484.html

时间: 2024-08-05 00:19:02

1351. Count Negative Numbers in a Sorted Matrix的相关文章

sorted matrix - search &amp; find-k-th

sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right corner, turn left or turn down, O(n+m) 2) if it's square, O(n+m) is optimal, see http://stackoverflow.com/a/10597806 3) if it's not square, e.g. in ex

Kth Smallest Number in Sorted Matrix

Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5. 分析: Although the matrix is horizontally and vertically sorted, there is no rule to find the next smalles

Interleaving Positive and Negative Numbers

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note: You are not necessary to keep the original order of positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6]

[Lintcode] Kth Smallest Number in Sorted Matrix

Kth Smallest Number in Sorted Matrix Find the kth smallest number in at row and column sorted matrix. Have you met this question in a real interview? Yes Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k lo

uva 10712 - Count the Numbers(数位dp)

题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a,b:问说在a到b之间有多少个n. 解题思路:数位dp,dp[i][j][x][y]表示第i位为j的时候,x是否前面是相等的,y是否已经出现过n.对于n=0的情况要特殊处理前导0,写的非常乱,搓死. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using na

Lintcode: Interleaving Positive and Negative Numbers 解题报告

Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-positive-and-negative-numbers/ Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. 注意 Yo

计算机中负数的表示法 Twos complement: Negative numbers in binary

Twos complement: Negative numbers in binary 二进制的负数表示法 负数的表示法: 我们将第一位定义为符号位 ,1代表负数 0代表正数 计算5+(-5) 结果是2 it doesn’t work, 下面我们来介绍 one’s complement 在one’s complement中进行相加: 结果都差了个1, two’s complement (将 -0 去掉了) 相加的结果也是正确的 two’s complement中,位对应的值也是make sens

[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 m

378. Kth Smallest Element in a Sorted Matrix

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/solutions http://www.cnblogs.com/EdwardLiu/p/6109080.html Heap : public class Solution { public int kthSmallest(int[][] matrix, int k) { int n = matrix.length; PriorityQueue<Tupl