generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
37 lines (32 loc) · 909 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <vector>
class Solution {
public:
int minimumSubarrayLength(std::vector<int>& nums, int k) {
std::size_t l{0}, r{0}, n{nums.size() + 1};
int ans{nums[0]};
int bitCounts[33] = {0};
for (int i{0}; (1 << i) <= nums[0]; ++i) {
if ((1 << i & nums[0]) == 0) continue;
++bitCounts[i];
}
while (true) {
if (ans >= k) {
if (r - l + 1 < n) n = r - l + 1;
if (l < r) {
for (int i{0}; (1 << i) <= nums[l]; ++i) {
if ((1 << i & nums[l]) == 0) continue;
if (--bitCounts[i] == 0) ans = ans & ~(1 << i);
}
++l;
continue;
}
}
if (++r >= nums.size()) break;
for (int i{0}; (1 << i) <= nums[r]; ++i) {
if ((1 << i & nums[r]) == 0) continue;
if (++bitCounts[i] == 1) ans = ans | (1 << i);
}
}
return n <= nums.size() ? n : -1;
}
};