In sorted nums, negatives are before first 0; positives are after last 0.
TimeComplexity:O(logn)
SpaceComplexity:O(1)
""" 2529.1 - Maximum Count of Positive Integer and Negative Integer - Solution 1 - Binary Search """
from bisect import bisect_left, bisect_right
from typing import List
class Solution:
def maximumCount(self, nums: List[int]) -> int:
neg = bisect_left(nums, 0)
pos = len(nums) - bisect_right(nums, 0)
return max(neg, pos)
function maximumCount(nums){
// nums sorted
let neg=0,pos=0;
for(const x of nums){ if(x<0) neg++; else if(x>0) pos++; }
return Math.max(neg,pos);
}
function maximumCount(nums:number[]):number{
let neg=0,pos=0;
for(const x of nums){ if(x<0) neg++; else if(x>0) pos++; }
return Math.max(neg,pos);
}
package main
func maximumCount(nums []int) int {
neg, pos := 0, 0
for _, x := range nums {
if x < 0 { neg++ } else if x > 0 { pos++ }
}
if neg > pos { return neg }
return pos
}
func main() {}
fn maximum_count(nums: &[i32]) -> i32 {
let (mut neg, mut pos) = (0, 0);
for &x in nums {
if x < 0 { neg += 1; }
else if x > 0 { pos += 1; }
}
neg.max(pos)
}
fn main() {}