20250116 finished.

This commit is contained in:
jackfiled 2025-01-16 22:19:32 +08:00
parent 583ef7fc06
commit 7abf8c3ccc
2 changed files with 63 additions and 0 deletions

View File

@ -434,3 +434,5 @@ mod p2270_number_of_ways_to_split_array;
mod p3065_minimum_operations_to_exceed_threshold_value_i;
mod p3066_minimum_operations_to_exceed_threshold_value_ii;
mod p3095_shortest_subarray_with_or_at_least_k_i;

View File

@ -0,0 +1,61 @@
/**
* [3095] Shortest Subarray With OR at Least K I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn minimum_subarray_length(nums: Vec<i32>, k: i32) -> i32 {
let mut bits = [0; 31];
let mut result = i32::MAX;
let check = |b: &[i32; 31]| {
let mut real_k = 0;
for (i, &v) in b.iter().enumerate() {
if v > 0 {
real_k |= 1 << i;
}
}
real_k >= k
};
let mut left = 0;
for right in 0..nums.len() {
for i in 0..31 {
bits[i] += nums[right] >> i & 1;
}
while left <= right && check(&bits) {
result = result.min((right - left + 1) as i32);
for i in 0..31 {
bits[i] -= nums[left] >> i & 1;
}
left += 1;
}
}
if result == i32::MAX {
-1
} else {
result
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3095() {
assert_eq!(1, Solution::minimum_subarray_length(vec![1, 2, 3], 2));
assert_eq!(3, Solution::minimum_subarray_length(vec![2, 1, 8], 10));
assert_eq!(1, Solution::minimum_subarray_length(vec![1, 2], 0));
}
}