-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3097.cpp
More file actions
31 lines (25 loc) · 800 Bytes
/
Copy path3097.cpp
File metadata and controls
31 lines (25 loc) · 800 Bytes
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
class Solution {
inline static int update(int count[32], int num, int value) {
int crnt = 0;
for (int k = 0; k < 32; k++) {
const auto bit = 1 << k;
if (num & bit) count[k] += value;
if (count[k]) crnt |= bit;
}
return crnt;
}
public:
int minimumSubarrayLength(const vector<int> &nums, int k) const {
static int count[32];
unsigned res = -1;
memset(count, 0x00, sizeof(count));
for (int i = 0, j = 0; j < size(nums); j++) {
int crnt = update(count, nums[j], 1);
for (; i <= j && crnt >= k; i++) {
res = min(res, (unsigned)(j - i + 1));
crnt = update(count, nums[i], -1);
}
}
return res;
}
};