problem:2401. 最长优雅子数组
ps:前置知识
Accode:
class Solution {
public:
int longestNiceSubarray(vector<int>& nums) {
int ans = 1;
int len = nums.size();
deque<int> deq;
int sum = 0;
int or_ = 0;
for(auto it:nums){
while(deq.size() && or_&it){
or_ ^= deq.front();
deq.pop_front();
sum--;
}
deq.push_back(it);
or_ |= it;
sum++;
ans = max(ans,sum);
}
return ans;
}
};
时间复杂度:$o(n)$