|
| 1 | +// 3097. Shortest Subarray With OR at Least K II |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii/ |
| 5 | +// |
| 6 | +// Tags: Array - Bit Manipulation - Sliding Window |
| 7 | + |
| 8 | +struct Solution; |
| 9 | +impl Solution { |
| 10 | + /// A nice variation of the sliding window problem where we need to figure out how to remove |
| 11 | + /// digits from the left when shrinking the window. To do it, we need to keep track of the |
| 12 | + /// number of times that we have seen a bit in values inside the window, when we pop a value, |
| 13 | + /// we need to remove one from that bit count, when we get to 0, that bit will not be set in |
| 14 | + /// the current value of the window's OR. |
| 15 | + /// |
| 16 | + /// Time complexity: O(n) - We visit each value in the input twice, for each, we iterate over |
| 17 | + /// the set bits in constant time because they max at 30. |
| 18 | + /// Space complexity: O(1) - One array of size 32 and a few integers. |
| 19 | + /// |
| 20 | + /// Runtime 23 ms Beats 100% |
| 21 | + /// Memory 4.06 MB Beats 66% |
| 22 | + pub fn minimum_subarray_length(nums: Vec<i32>, k: i32) -> i32 { |
| 23 | + let mut l = 0; |
| 24 | + let mut res = usize::MAX; |
| 25 | + let mut bits = [0; 32]; |
| 26 | + let mut mask; |
| 27 | + let mut int_val = 0; |
| 28 | + for (r, &num) in nums.iter().enumerate() { |
| 29 | + // Print integer as binary digits. |
| 30 | + // println!("num: {:#032b} - {}", num, num); |
| 31 | + for i in 0..32 { |
| 32 | + mask = 1 << i; |
| 33 | + // If this bit is set in the current num. |
| 34 | + if num & mask > 0 { |
| 35 | + // Update the count of numbers with this bit set. |
| 36 | + bits[i] += 1; |
| 37 | + // If we go from 0 to 1, update the int value. |
| 38 | + if bits[i] == 1 { |
| 39 | + int_val += mask; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + // Now shrink the window as much as we can. |
| 44 | + while int_val >= k && l <= r { |
| 45 | + res = res.min(1 + r - l); |
| 46 | + for i in 0..32 { |
| 47 | + mask = 1 << i; |
| 48 | + if nums[l] & mask > 0 { |
| 49 | + bits[i] -= 1; |
| 50 | + if bits[i] == 0 { |
| 51 | + int_val -= mask; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + l += 1; |
| 56 | + } |
| 57 | + } |
| 58 | + if res == usize::MAX { |
| 59 | + -1 |
| 60 | + } else { |
| 61 | + res as i32 |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Tests. |
| 67 | +fn main() { |
| 68 | + let tests = [ |
| 69 | + (vec![1, 2], 0, 1), |
| 70 | + (vec![1, 2, 3], 2, 1), |
| 71 | + (vec![2, 1, 8], 10, 3), |
| 72 | + (vec![1, 2, 32, 21], 55, 3), |
| 73 | + ]; |
| 74 | + println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len()); |
| 75 | + let mut success = 0; |
| 76 | + for (i, t) in tests.iter().enumerate() { |
| 77 | + let res = Solution::minimum_subarray_length(t.0.clone(), t.1); |
| 78 | + if res == t.2 { |
| 79 | + success += 1; |
| 80 | + println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i); |
| 81 | + } else { |
| 82 | + println!( |
| 83 | + "\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m", |
| 84 | + i, t.2, res |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + println!(); |
| 89 | + if success == tests.len() { |
| 90 | + println!("\x1b[30;42m✔ All tests passed!\x1b[0m") |
| 91 | + } else if success == 0 { |
| 92 | + println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m") |
| 93 | + } else { |
| 94 | + println!( |
| 95 | + "\x1b[31mx\x1b[95m {} tests failed!\x1b[0m", |
| 96 | + tests.len() - success |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
0 commit comments