/** * Definition for BigArray. * class BigArray { * public BigArray(int[] elements); * public int at(long index); * public long size(); * } */classSolution{publicintcountBlocks(BigArraynums){intans=0;for(longi=0,n=nums.size();i<n;++ans){i=search(nums,i,n);}returnans;}privatelongsearch(BigArraynums,longl,longn){longr=n;intx=nums.at(l);while(l<r){longmid=(l+r)>>1;if(nums.at(mid)!=x){r=mid;}else{l=mid+1;}}returnl;}}
/** * Definition for BigArray. * class BigArray { * public: * BigArray(vector<int> elements); * int at(long long index); * long long size(); * }; */classSolution{public:intcountBlocks(BigArray*nums){intans=0;usingll=longlong;lln=nums->size();autosearch=[&](lll){llr=n;intx=nums->at(l);while(l<r){llmid=(l+r)>>1;if(nums->at(mid)!=x){r=mid;}else{l=mid+1;}}returnl;};for(longlongi=0;i<n;++ans){i=search(i);}returnans;}};
/** * Definition for BigArray. * class BigArray { * public BigArray(int[] elements); * public int at(long index); * public long size(); * } */classSolution{publicintcountBlocks(BigArraynums){returnf(nums,0,nums.size()-1);}privateintf(BigArraynums,longl,longr){if(nums.at(l)==nums.at(r)){return1;}longmid=(l+r)>>1;inta=f(nums,l,mid);intb=f(nums,mid+1,r);returna+b-(nums.at(mid)==nums.at(mid+1)?1:0);}}
1 2 3 4 5 6 7 8 910111213141516171819202122232425
/** * Definition for BigArray. * class BigArray { * public: * BigArray(vector<int> elements); * int at(long long index); * long long size(); * }; */classSolution{public:intcountBlocks(BigArray*nums){usingll=longlong;function<int(ll,ll)>f=[&](lll,llr){if(nums->at(l)==nums->at(r)){return1;}llmid=(l+r)>>1;inta=f(l,mid);intb=f(mid+1,r);returna+b-(nums->at(mid)==nums->at(mid+1));};returnf(0,nums->size()-1);}};
1 2 3 4 5 6 7 8 91011121314151617181920
/** * Definition for BigArray. * class BigArray { * constructor(elements: number[]); * public at(index: number): number; * public size(): number; * } */functioncountBlocks(nums:BigArray|null):number{constf=(l:number,r:number):number=>{if(nums.at(l)===nums.at(r)){return1;}constmid=l+Math.floor((r-l)/2);consta=f(l,mid);constb=f(mid+1,r);returna+b-(nums.at(mid)===nums.at(mid+1)?1:0);};returnf(0,nums.size()-1);}