-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1351.py
57 lines (46 loc) · 1.41 KB
/
1351.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
54
55
56
57
# [ LeetCode ] 1351. Count Negative Numbers in a Sorted Matrix
def solution(grid: list[list[int]]) -> int:
answer: int = 0
M, N = len(grid), len(grid[0])
for i in range(M):
start, end = 0, N - 1
while start <= end:
middle: int = start + (end - start) // 2
if grid[i][middle] >= 0:
start = middle + 1
else:
end = middle - 1
answer += (N - start)
return answer
def another_solution(grid: list[list[int]]) -> int:
answer: int = 0
M, N = len(grid), len(grid[0])
left, right = 0, M - 1
while left <= (N - 1) and right >= 0:
if grid[right][left] >= 0:
left += 1
else:
answer += (N - left)
right -= 1
return answer
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[list[int]]] | int]] = [
{
"input": {
"grid": [
[4, 3, 2, -1],
[3, 2, 1, -1],
[1, 1, -1, -2],
[-1, -1, -2, -3]
]
},
"output": 8
},
{
"input": {"grid": [[3, 2], [1, 0]]},
"output": 0
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
# assert case["output"] == another_solution(**case["input"])