1351. Count Negative Numbers in a Sorted Matrix
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
Python
class Solution(object):
def countNegatives(self, grid):
m, n = len(grid), len(grid[0])
count = 0
for i in range(m):
if grid[i][0] < 0:
count += (m-i)*n
break
for j in range(n):
if grid[i][j] < 0:
count += n-j
break
return count
Go
class Solution(object):
def countNegatives(self, grid):
m, n = len(grid), len(grid[0])
count = 0
for i in range(m):
if grid[i][0] < 0:
count += (m-i)*n
break
for j in range(n):
if grid[i][j] < 0:
count += n-j
break
return count
Rust
impl Solution {
pub fn count_negatives(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut count = 0;
for i in 0..m {
if grid[i][0] < 0 {
count += ((m-i) * n) as i32;
break;
}
for j in 0..n {
if grid[i][j] < 0 {
count += (n-j) as i32;
break;
}
}
}
count
}
}