-
Notifications
You must be signed in to change notification settings - Fork 0
/
2529.py
68 lines (46 loc) · 1.57 KB
/
2529.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
58
59
60
61
62
63
64
65
66
67
68
from typing import List
import unittest
def binary_search_negatives(nums, size):
right = size - 1
left = 0
while left <= right:
middle = (left + right) // 2
if nums[middle] > 0:
right -= 1
elif nums[middle] < 0:
left += 1
else:
right -= 1
return left
def binary_search_positives(nums, left, size):
right = size - 1
while left <= right:
middle = (left + right) // 2
if nums[middle] <= 0:
left += 1
elif nums[middle] > 0:
right -= 1
return size - left
class Solution:
def maximumCount(self, nums: List[int]) -> int:
size = len(nums)
negatives = binary_search_negatives(nums, size)
positives = binary_search_positives(nums, negatives, size)
return max(negatives, positives)
class Test(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_first(self):
self.assertEqual(self.solution.maximumCount(nums=[-2, -1, -1, 1, 2, 3]), 3)
def test_second(self):
self.assertEqual(self.solution.maximumCount(nums=[5, 20, 66, 1314]), 4)
def test_third(self):
self.assertEqual(self.solution.maximumCount(nums=[-3, -2, -1, 0, 0, 1, 2]), 3)
def test_fourth(self):
nums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(self.solution.maximumCount(nums), 0)
def test_fifth(self):
nums = [-2, -1, -1, 0, 0, 0]
self.assertEqual(self.solution.maximumCount(nums), 3)
if __name__ == "__main__":
unittest.main()