Skip to content

Commit 184887f

Browse files
committed
Time: 163 ms (8.57%), Space: 61.1 MB (28.27%) - LeetHub
1 parent f7bb89c commit 184887f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int longestNiceSubarray(vector<int>& nums) {
4+
vector<int> on(33);
5+
6+
auto op = [&](int x, int y) {
7+
for (int i = 0; i < 31; i++) {
8+
if ((1 << i) & x) {
9+
on[i] += y;
10+
}
11+
}
12+
};
13+
14+
auto ok = [&](int x) {
15+
for (int i = 0; i < 31; i++) {
16+
if ((1 << i) & x) {
17+
if (on[i]) return false;
18+
}
19+
}
20+
return true;
21+
};
22+
23+
int n = nums.size();
24+
int ans = 0;
25+
for (int l = 0, r = 0; r < n; r++) {
26+
while (!ok(nums[r])) {
27+
op(nums[l], -1);
28+
l++;
29+
}
30+
op(nums[r], 1);
31+
ans = max(ans, r - l + 1);
32+
}
33+
return ans;
34+
}
35+
};

0 commit comments

Comments
 (0)