Mắt Xích Yếu
Senior Member
Java:
class Solution {
public int minimumSubarrayLength(int[] nums, int k) {
int res = Integer.MAX_VALUE;
int n = nums.length;
int or = 0;
int[] cur_state = new int[33];
int i = 0;
int l = 0;
for (int r = 0; r < n; r++) {
or |= nums[r];
int num = nums[r];
i = 0;
while (num > 0) {
cur_state[i++] += num & 1;
num >>= 1;
}
if (or>=k) {
while (l <= r && or>=k) {
int num_left = nums[l];
i = 0;
while (num_left > 0) {
cur_state[i] -= num_left & 1;
if(cur_state[i]==0){
or&=~(1<<i);//flip ith bit to 0
}
i++;
num_left >>= 1;
}
l++;
}
res = Math.min(res, r - l+ 2);
}
}
return res==Integer.MAX_VALUE?-1:res;
}
}
.
xài python quen rồi ko thèm để ý mấy cái integer limit, giờ đổi qua c++ mất 1 đấm vì không cast qua long long
.

