Skip to content

Commit 6deb789

Browse files
authored
Create count-negative-numbers-in-a-sorted-matrix.py
1 parent e4cd85e commit 6deb789

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Time: O(m + n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def countNegatives(self, grid):
6+
"""
7+
:type grid: List[List[int]]
8+
:rtype: int
9+
"""
10+
result, c = 0, len(grid[0])-1
11+
for row in grid:
12+
while c >= 0 and row[c] < 0:
13+
c -= 1
14+
result += len(grid[0])-1-c
15+
return result

0 commit comments

Comments
 (0)