-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path3097. Shortest Subarray With OR at Least K II
53 lines (46 loc) · 1.21 KB
/
3097. Shortest Subarray With OR at Least K II
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
int minimumSubarrayLength(vector<int>& nums, int k) {
int n = nums.size();
int z = *max_element(nums.begin(), nums.end());
if(z >= k)
return 1;
int ans = INT_MAX;
int x = nums[0];
int i = 0;
int j = 1;
while(j < n)
{
x |= nums[j];
if(x < k)
{
j++;
}
else
{
ans = min(ans, j - i + 1);
while(i < n && i <= j && x >= k)
{
ans = min(ans, j - i + 1);
if(nums[i] == nums[i+1])
{
i++;
}
else
{
i++;
x = nums[i];
for(int t = i + 1; t <= j; t++)
{
x |= nums[t];
}
}
}
j++;
}
}
if(ans == INT_MAX)
return -1;
return ans;
}
};