Skip to content

Commit 7810d50

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18.3 MB (22.96%)
1 parent b46f3fd commit 7810d50

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>Given an array <code>nums</code> sorted in <strong>non-decreasing</strong> order, return <em>the maximum between the number of positive integers and the number of negative integers.</em></p>
2+
3+
<ul>
4+
<li>In other words, if the number of positive integers in <code>nums</code> is <code>pos</code> and the number of negative integers is <code>neg</code>, then return the maximum of <code>pos</code> and <code>neg</code>.</li>
5+
</ul>
6+
7+
<p><strong>Note</strong> that <code>0</code> is neither positive nor negative.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> nums = [-2,-1,-1,1,2,3]
14+
<strong>Output:</strong> 3
15+
<strong>Explanation:</strong> There are 3 positive integers and 3 negative integers. The maximum count among them is 3.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [-3,-2,-1,0,0,1,2]
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> There are 2 positive integers and 3 negative integers. The maximum count among them is 3.
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [5,20,66,1314]
30+
<strong>Output:</strong> 4
31+
<strong>Explanation:</strong> There are 4 positive integers and 0 negative integers. The maximum count among them is 4.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= nums.length &lt;= 2000</code></li>
39+
<li><code>-2000 &lt;= nums[i] &lt;= 2000</code></li>
40+
<li><code>nums</code> is sorted in a <strong>non-decreasing order</strong>.</li>
41+
</ul>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(log(n))</code> time complexity?</p>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Approach 2: Binary Search
2+
3+
# Time: O(log n)
4+
# Space: O(1)
5+
6+
from bisect import bisect_right, bisect_left
7+
8+
class Solution:
9+
def maximumCount(self, nums: List[int]) -> int:
10+
return max(len(nums) - bisect_right(nums, 0), bisect_left(nums, 0))
11+

0 commit comments

Comments
 (0)