-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1351-count_neg_nums_in_sorted_matrix.py
53 lines (44 loc) · 1.65 KB
/
1351-count_neg_nums_in_sorted_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
"""
class Solution(object):
"""
Just the brute force, O(m * n)
"""
def countNegatives(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
length = len(grid) #positions rows
height = len(grid[0]) #positions cols
counter = 0
for row in range(length):
for col in range(height):
if grid[row][col] < 0:
counter += 1
return counter
"""
"Snaking" method, where you start in the bottom left corner
and zig zag your way up to the top right
With implementation help from: https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/510249/JavaPython-3-2-similar-O(m-%2B-n)-codes-w-brief-explanation-and-analysis.
O(n + m) / linear time, where n = height, m = length
Runtime: 88 ms, faster than 99.73% of Python online submissions for Count Negative Numbers in a Sorted Matrix.
Memory Usage: 14.6 MB, less than 9.57% of Python online submissions for Count Negative Numbers in a Sorted Matrix.
"""
def countNegatives(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
length = len(grid) #positions rows
height = len(grid[0]) #positions cols
row, col = 0, height - 1
result = 0
while row < length and col >= 0:
if grid[row][col] < 0:
result += length - row
col -= 1
else:
row += 1
return result