Re: [闲聊] 每日leetcode

楼主: dont   2024-11-10 11:03:45
3097. Shortest Subarray With OR at Least K II
## 思路
sliding window纪录每个bit的个数
检查or的val, 如果val >= k 就更新window left
## Code
```python
class Solution:
def minimumSubarrayLength(self, nums: List[int], k: int) -> int:
if k == 0:
return 1
n = len(nums)
res = float('inf')
count = [0] * 32
left = 0
for right in range(n):
val = 0
for i in range(32):
if nums[right] & (1 << i):
count[i] += 1
if count[i]:
val |= (1 << i)
while val >= k:
res = min(res, right - left + 1)
val = 0
for i in range(32):
if nums[left] & (1 << i):
count[i] -= 1
if count[i]:
val |= (1 << i)
left += 1
return -1 if res == float('inf') else res
```

Links booklink

Contact Us: admin [ a t ] ucptt.com