20250117 finished.
This commit is contained in:
parent
7abf8c3ccc
commit
402c045436
|
@ -436,3 +436,5 @@ 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;
|
||||
|
||||
mod p3097_shortest_subarray_with_or_at_least_k_ii;
|
||||
|
|
61
src/problem/p3097_shortest_subarray_with_or_at_least_k_ii.rs
Normal file
61
src/problem/p3097_shortest_subarray_with_or_at_least_k_ii.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
/**
|
||||
* [3097] Shortest Subarray With OR at Least K II
|
||||
*/
|
||||
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_3097() {
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user