3380. Shortest Subarray With OR at Least K II
My accepted Python solution to LeetCode problem 3380, Shortest Subarray With OR at Least K II, running in 1824ms.
- Difficulty: Medium
- Python
- Runtime 1824ms
- Memory 34.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1824ms, memory 34.2MB, accepted 2025-12-29.
class Solution:
def minimumSubarrayLength(self, nums: List[int], k: int) -> int:
# Sliding window with bit count tracking
# Time: O(n * 30), Space: O(30) = O(1)
n = len(nums)
if k == 0:
return 1
# Track count of each bit in current window
bit_count = [0] * 30
def get_or():
result = 0
for i in range(30):
if bit_count[i] > 0:
result |= (1 << i)
return result
def add(num):
for i in range(30):
if num & (1 << i):
bit_count[i] += 1
def remove(num):
for i in range(30):
if num & (1 << i):
bit_count[i] -= 1
min_len = float('inf')
left = 0
for right in range(n):
add(nums[right])
while left <= right and get_or() >= k:
min_len = min(min_len, right - left + 1)
remove(nums[left])
left += 1
return min_len if min_len != float('inf') else -1