3097. Shortest Subarray With OR at Least K II
You are given an array nums of non-negative integers and an integer k.
An array is called special if the bitwise OR of all of its elements is at least k.
Return the length of the shortest special non-empty subarray of nums, or return -1 if no special subarray exists.
Example 1:
Input: nums = [1,2,3], k = 2
Output: 1
Explanation:
The subarray [3] has OR value of 3. Hence, we return 1.
Example 2:
Input: nums = [2,1,8], k = 10
Output: 3
Explanation:
The subarray [2,1,8] has OR value of 11. Hence, we return 3.
Example 3:
Input: nums = [1,2], k = 0
Output: 1
Explanation:
The subarray [1] has OR value of 1. Hence, we return 1.
Constraints:
1 <= nums.length <= 2 * 1050 <= nums[i] <= 1090 <= k <= 109
C++
class Solution { public: int minimumSubarrayLength(vector<int>& nums, int k) { int n = nums.size(); int cnt[32]{}; int ans = n + 1; for (int i = 0, j = 0, s = 0; j < n; ++j) { s |= nums[j]; for (int h = 0; h < 32; ++h) { if ((nums[j] >> h & 1) == 1) { ++cnt[h]; } } for (; s >= k && i <= j; ++i) { ans = min(ans, j - i + 1); for (int h = 0; h < 32; ++h) { if ((nums[i] >> h & 1) == 1) { if (--cnt[h] == 0) { s ^= 1 << h; } } } } } return ans > n ? -1 : ans; } };
JAVA
class Solution { public int minimumSubarrayLength(int[] nums, int k) { int n = nums.length; int[] cnt = new int[32]; int ans = n + 1; for (int i = 0, j = 0, s = 0; j < n; ++j) { s |= nums[j]; for (int h = 0; h < 32; ++h) { if ((nums[j] >> h & 1) == 1) { ++cnt[h]; } } for (; s >= k && i <= j; ++i) { ans = Math.min(ans, j - i + 1); for (int h = 0; h < 32; ++h) { if ((nums[i] >> h & 1) == 1) { if (--cnt[h] == 0) { s ^= 1 << h; } } } } } return ans > n ? -1 : ans; } }
PYTHON
from typing import List class Solution: def minimumSubarrayLength(self, nums: List[int], k: int) -> int: n = len(nums) cnt = [0] * 32 ans = n + 1 i = s = 0 for j in range(n): s |= nums[j] for h in range(32): if (nums[j] >> h) & 1: cnt[h] += 1 while s >= k and i <= j: ans = min(ans, j - i + 1) for h in range(32): if (nums[i] >> h) & 1: cnt[h] -= 1 if cnt[h] == 0: s ^= 1 << h i += 1 return -1 if ans > n else ans
Comments
Post a Comment