Posted on

2529. Maximum Count of Positive Integer and Negative Integer

So the daily leetcode challenge can be naively solved with a loop.
But the best solution in the python category uses a built in method called bisect_left, so here is a binary search implementation of that method in JavaScript and the solution that followed.


const bisectLeft = (array, x, lo = 0, hi = array.length ) => {
    while( lo < hi ){
        const mid = lo + ((hi-lo)>>1);
        if( array[mid] < x )
            lo = mid + 1;
        else
            hi = mid;
    }
    return lo;
}
function maximumCount(nums: number[]): number {
    const firstNonNegative = bisectLeft(nums, 0)
    const firstPositive = bisectLeft(nums, 1)
    const negativeCount = firstNonNegative;
    const positiveCount = nums.length - firstPositive; 
    return Math.max( negativeCount, positiveCount );
    
};