2529. 正整数和负整数的最大计数
思路:通过二分查找实现0(logn)的时间复杂度
class Solution {
public:
int maximumCount(vector<int>& nums) {
int n=nums.size();
int l=0,r=n-1;
while(l<r){
int mid=(l+r+1)/2;
if(nums[mid]<0) l=mid;
else r=mid-1;
}
int ct1,ct2;
if(nums[l]<0) ct1=l+1;
else ct1=0;
l=0,r=n-1;
while(l<r){
int mid=(l+r)/2;
if(nums[mid]>0) r=mid;
else l=mid+1;
}
if(nums[l]>0) ct2=n-l;
else ct2=0;
return max(ct1,ct2);
}
};