https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer
class Solution {
public int maximumCount(int[] nums) {
// 思路:由于数组非递减,考虑logn实现
// 因此可以有二段性可以二分
// 即寻找正数和负数的分界点target
// 答案就是max(target,nums.length-targ1et)
int target=lowbound(nums,1); // 寻找第一个正数,即分界点
int target2=lowbound(nums,0)-1; // 寻找最后一个负数,即分界点
System.out.println(target2+" "+target);
return Math.max(target2+1,nums.length-target);
}
int lowbound(int[] nums,int x)
{
int l=0,r=nums.length-1;
while(l<=r)
{
int mid=l+(r-l>>1);
if(nums[mid]<x)l=mid+1;
else r=mid-1;
}
return l;
}
}
标签:正整数,target,nums,int,2529,length,target2,leetcode,lowbound From: https://www.cnblogs.com/lxl-233/p/18530705