题目描述:

image-20250116010001388

解析:之前做的按位与简单版,直接暴力两重循环,result记录数组长度,如果没有值就为Integer.MAX_VALUE

代码:

package January;

/**
* @author hxw
* @version 1.0
* @date 2025/1/16 0:40
* @description: 3095. 或值至少 K 的最短子数组 I 简单
*/
public class sixteen {
public static void main(String[] args) {
int result = sixteen.Solution.minimumSubarrayLength(new int[]{1, 2, 3}, 2);
System.out.println(result);
}

class Solution {
public static int minimumSubarrayLength(int[] nums, int k) {
int result = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
int value = nums[i];
for (int j = i; j < nums.length; j++) {
value = value | nums[j];
if (value >= k) {
result = result > j - i + 1 ? j - i + 1 : result;
break;
}
}

}
return result == Integer.MAX_VALUE ? -1 : result;
}
}

}