Leetcode Note: Go - Count Negative Numbers in a Sorted Matrix
Count Negative Numbers in a Sorted Matrix - LeetCode
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
- Go 言語で取り組んだメモ
回答
Golang - O(m+n) Solution 8ms - Count Negative Numbers in a Sorted Matrix - LeetCode
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/solutions/984098/golang-o-m-n-solution-8ms/
func countNegatives(grid [][]int) int {
m, n := len(grid), len(grid[0])
r, c := 0, n-1
ans := 0
for r < m && c >= 0 {
if grid[r][c] >= 0 {
r++
} else {
ans += m-r
c--
}
}
return ans
}