/* * @Author: LetMeFly * @Date: 2025-01-17 12:08:16 * @LastEditors: LetMeFly.xyz * @LastEditTime: 2025-01-17 12:22:14 */ classSolution { private: intcalc(int* cnt){ int ans = 0; for (int i = 0; i < 30; i++) { if (cnt[i]) { ans |= 1 << i; } } return ans; }
voidadd(int* cnt, int a){ for (int i = 0; i < 30; i++) { cnt[i] += a >> i & 1; } }
voiddel(int* cnt, int a){ for (int i = 0; i < 30; i++) { cnt[i] -= a >> i & 1; } } public: intminimumSubarrayLength(vector<int>& nums, int k){ if (!k) { return1; } int ans = 1000000; int cnt[30] = {0}; for (int l = 0, r = 0; l < nums.size(); l++) { if (l > 0) { del(cnt, nums[l - 1]); } while (r < nums.size() && calc(cnt) < k) { add(cnt, nums[r++]); } if (calc(cnt) >= k) { ans = min(ans, r - l); } } return ans == 1000000 ? -1 : ans; } };
classSolution: defcalc(self) -> int: ans = 0 for i inrange(30): ifself.cnt[i]: ans += 1 << i return ans
defadd(self, a: int) -> None: for i inrange(30): self.cnt[i] += a >> i & 1
defsub(self, a: int) -> None: for i inrange(30): self.cnt[i] -= a >> i & 1
defminimumSubarrayLength(self, nums: List[int], k: int) -> int: ifnot k: return1 ans = 1000000 r = 0 self.cnt = [0] * 30 for l inrange(len(nums)): if l: self.sub(nums[l - 1]) while r < len(nums) andself.calc() < k: self.add(nums[r]) r += 1 ifself.calc() >= k: ans = min(ans, r - l) return -1if ans == 1000000else ans
funccalc(cnt []int) (ans int) { for i, v := range cnt { if v > 0 { ans |= 1 << i } } return }
funcadd(cnt []int, a int) { for i := range cnt { cnt[i] += a >> i & 1 } }
funcdel(cnt []int, a int) { for i := range cnt { cnt[i] -= a >> i & 1 } }
funcminimumSubarrayLength(nums []int, k int)int { if k == 0 { return1 } ans := 1000000 cnt := make([]int, 30) for l, r := 0, 0; l < len(nums); l++ { if l > 0 { del(cnt, nums[l - 1]) } for r < len(nums) && calc(cnt) < k { add(cnt, nums[r]) r++ } if calc(cnt) >= k { ans = min(ans, r - l) } } if ans == 1000000 { return-1 } return ans }