Skip to content

Commit 780e8aa

Browse files
authored
Create 3097. Shortest Subarray With OR at Least K II (#631)
2 parents 13a11ed + eedef8a commit 780e8aa

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
public:
3+
int minimumSubarrayLength(vector<int>& nums, int k) {
4+
5+
int n = nums.size();
6+
int z = *max_element(nums.begin(), nums.end());
7+
8+
if(z >= k)
9+
return 1;
10+
11+
int ans = INT_MAX;
12+
int x = nums[0];
13+
int i = 0;
14+
int j = 1;
15+
16+
while(j < n)
17+
{
18+
x |= nums[j];
19+
if(x < k)
20+
{
21+
j++;
22+
}
23+
else
24+
{
25+
ans = min(ans, j - i + 1);
26+
while(i < n && i <= j && x >= k)
27+
{
28+
ans = min(ans, j - i + 1);
29+
30+
if(nums[i] == nums[i+1])
31+
{
32+
i++;
33+
}
34+
else
35+
{
36+
i++;
37+
x = nums[i];
38+
for(int t = i + 1; t <= j; t++)
39+
{
40+
x |= nums[t];
41+
}
42+
}
43+
}
44+
j++;
45+
}
46+
}
47+
48+
if(ans == INT_MAX)
49+
return -1;
50+
51+
return ans;
52+
}
53+
};

0 commit comments

Comments
 (0)