Skip to content

Commit f898e6e

Browse files
committed
LC 3097. Shortest Subarray With OR at Least K II (Rust)
1 parent 9930709 commit f898e6e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ to the solution in this repository.
782782
| [3043. Find the Length of the Longest Common Prefix][lc3043] | 🟠 Medium | [![rust](res/rs.png)][lc3043rs] |
783783
| [3068. Find the Maximum Sum of Node Values][lc3068] | 🔴 Hard | [![rust](res/rs.png)][lc3068rs] |
784784
| [3075. Maximize Happiness of Selected Children][lc3075] | 🟠 Medium | [![rust](res/rs.png)][lc3075rs] |
785+
| [3097. Shortest Subarray With OR at Least K II][lc3097] | 🟠 Medium | [![rust](res/rs.png)][lc3097rs] |
785786
| [3110. Score of a String][lc3110] | 🟢 Easy | [![rust](res/rs.png)][lc3110rs] |
786787
| [3133. Minimum Array End][lc3133] | 🟠 Medium | [![rust](res/rs.png)][lc3133rs] |
787788
| [3217. Delete Nodes From Linked List Present in Array][lc3217] | 🟠 Medium | [![python](res/py.png)][lc3217py] |
@@ -2502,6 +2503,8 @@ to the solution in this repository.
25022503
[lc3068rs]: leetcode/find-the-maximum-sum-of-node-values.rs
25032504
[lc3075]: https://leetcode.com/problems/maximize-happiness-of-selected-children/
25042505
[lc3075rs]: leetcode/maximize-happiness-of-selected-children.rs
2506+
[lc3097]: https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii/
2507+
[lc3097rs]: leetcode/shortest-subarray-with-or-at-least-k-ii.rs
25052508
[lc3110]: https://leetcode.com/problems/score-of-a-string/
25062509
[lc3110rs]: leetcode/score-of-a-string.rs
25072510
[lc3133]: https://leetcode.com/problems/minimum-array-end/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)