Skip to content

Commit

Permalink
Add problem 2411: Smallest Subarrays With Maximum Bitwise OR
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 26, 2025
1 parent 34e5c8e commit 1fa4951
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,7 @@ pub mod problem_2405_optimal_partition_of_string;
pub mod problem_2406_divide_intervals_into_minimum_number_of_groups;
pub mod problem_2409_count_days_spent_together;
pub mod problem_2410_maximum_matching_of_players_with_trainers;
pub mod problem_2411_smallest_subarrays_with_maximum_bitwise_or;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
let mut one_indices = [0_u32; 32];
let mut nums = nums;

for (i, target) in nums.iter_mut().enumerate().rev() {
let i = i as u32;
let mut value = *target;

while value != 0 {
one_indices[value.trailing_zeros() as usize] = i;
value &= value - 1;
}

*target = (one_indices.iter().copied().max().unwrap().saturating_sub(i) + 1) as _;
}

nums
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
Self::smallest_subarrays(nums)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
36 changes: 36 additions & 0 deletions src/problem_2411_smallest_subarrays_with_maximum_bitwise_or/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub mod greedy;

pub trait Solution {
fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32>;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(&[1, 0, 2, 1, 3] as &[_], &[3, 3, 2, 2, 1] as &[_]),
(&[1, 2], &[2, 1]),
(&[0], &[1]),
(&[1_000_000_000], &[1]),
(
&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 32, 2, 0, 0, 0, 0, 0, 0, 28, 16, 32, 0, 0, 0, 0,
0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 2, 0, 0,
32, 0, 0, 0, 4, 16, 0, 4, 0, 32, 0, 0, 8, 0, 1, 1, 0, 0, 0, 0, 0,
],
&[
36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 43, 42, 41, 40, 39, 38,
37, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30,
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
3, 2, 1, 1, 1, 1, 1, 1, 1,
],
),
];

for (nums, expected) in test_cases {
assert_eq!(S::smallest_subarrays(nums.to_vec()), expected);
}
}
}

0 comments on commit 1fa4951

Please sign in to comment.