3097.cpp (800B)
1 class Solution { 2 inline static int update(int count[32], int num, int value) { 3 int crnt = 0; 4 5 for (int k = 0; k < 32; k++) { 6 const auto bit = 1 << k; 7 if (num & bit) count[k] += value; 8 if (count[k]) crnt |= bit; 9 } 10 11 return crnt; 12 } 13 14 public: 15 int minimumSubarrayLength(const vector<int> &nums, int k) const { 16 static int count[32]; 17 unsigned res = -1; 18 19 memset(count, 0x00, sizeof(count)); 20 for (int i = 0, j = 0; j < size(nums); j++) { 21 int crnt = update(count, nums[j], 1); 22 23 for (; i <= j && crnt >= k; i++) { 24 res = min(res, (unsigned)(j - i + 1)); 25 crnt = update(count, nums[i], -1); 26 } 27 } 28 29 return res; 30 } 31 };