-
Notifications
You must be signed in to change notification settings - Fork 0
/
1351.py
57 lines (41 loc) · 1.24 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
from typing import List
import unittest
def binary_search_for_the_first_negative_number(nums: List[int], size: int) -> int:
left = 0
right = size - 1
while left <= right:
middle = (left + right) // 2
if nums[middle] > 0 or nums[middle] == 0:
left += 1
elif nums[middle] < 0:
right -= 1
return size - left
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
size = len(grid[0])
for row in grid:
count += binary_search_for_the_first_negative_number(row, size)
return count
class Test(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_first(self):
self.assertEqual(
self.solution.countNegatives(
grid=[[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]]
),
8,
)
def test_second(self):
self.assertEqual(
self.solution.countNegatives(grid=[[3, 2], [1, 0]]),
0,
)
def test_third(self):
self.assertEqual(
self.solution.countNegatives(grid=[[1, -1], [-1, -1]]),
3,
)
if __name__ == "__main__":
unittest.main()