feat: 2044_count_number_of_maximum_bitwise_or_subsets
This commit is contained in:
parent
d5635e7618
commit
ef53a58935
7
2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock
generated
Normal file
7
2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock
generated
Normal file
@ -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"
|
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "count_number_of_maximum_bitwise_or_subsets"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
37
2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs
Normal file
37
2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
pub struct Solution;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
|
||||||
|
let max_or_sum = nums.iter().fold(0, |acc, &num| acc | num);
|
||||||
|
|
||||||
|
let mut result = 0;
|
||||||
|
let mut stack: Vec<DFSParams> = 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,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user