From d540894ae0590bc51f79025cfac6baac97e53e10 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Wed, 20 Aug 2025 01:07:52 +0800 Subject: [PATCH] feat: add initial implementation of Cargo.toml, Cargo.lock, and lib.rs for count_max_or_subsets function --- .../Cargo.lock | 7 ++++ .../Cargo.toml | 6 +++ .../src/lib.rs | 37 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock create mode 100644 2044_count_number_of_maximum_bitwise_or_subsets/Cargo.toml create mode 100644 2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs diff --git a/2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock b/2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock new file mode 100644 index 0000000..fa0e165 --- /dev/null +++ b/2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "count_number_of_maximum_bitwise_or_subsets" +version = "0.1.0" diff --git a/2044_count_number_of_maximum_bitwise_or_subsets/Cargo.toml b/2044_count_number_of_maximum_bitwise_or_subsets/Cargo.toml new file mode 100644 index 0000000..e225f6f --- /dev/null +++ b/2044_count_number_of_maximum_bitwise_or_subsets/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "count_number_of_maximum_bitwise_or_subsets" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs b/2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs new file mode 100644 index 0000000..5f4e80b --- /dev/null +++ b/2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs @@ -0,0 +1,37 @@ +pub struct Solution; + +impl Solution { + pub fn count_max_or_subsets(nums: Vec) -> i32 { + let max_or_sum = nums.iter().fold(0, |acc, &num| acc | num); + + let mut result = 0; + let mut stack: Vec = nums + .iter() + .enumerate() + .map(|(index, &num)| DFSParams { index, or_sum: num }) + .collect(); + + while !stack.is_empty() { + let current = stack.pop().unwrap(); + + if current.or_sum == max_or_sum { + result += 1 << (nums.len() - current.index - 1); + continue; + } + + for i in (current.index + 1)..nums.len() { + stack.push(DFSParams { + index: i, + or_sum: current.or_sum | nums[i], + }); + } + } + + result + } +} + +pub struct DFSParams { + index: usize, + or_sum: i32, +}