20250112 finished.
This commit is contained in:
parent
2bdecf9e90
commit
676b28a67e
|
@ -426,3 +426,5 @@ mod p3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i;
|
|||
mod p3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii;
|
||||
|
||||
mod p3270_find_the_key_of_the_numbers;
|
||||
|
||||
mod p2275_largest_combination_with_bitwise_and_greater_than_zero;
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* [2275] Largest Combination With Bitwise AND Greater Than Zero
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn largest_combination(candidates: Vec<i32>) -> i32 {
|
||||
let mut result = 0;
|
||||
|
||||
for i in 0..31 {
|
||||
result = result.max(
|
||||
candidates
|
||||
.iter()
|
||||
.filter_map(|&x| if x & (1 << i) > 0 { Some(()) } else { None })
|
||||
.count(),
|
||||
);
|
||||
}
|
||||
|
||||
result as i32
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2275() {
|
||||
assert_eq!(
|
||||
4,
|
||||
Solution::largest_combination(vec![16, 17, 71, 62, 12, 24, 14])
|
||||
);
|
||||
assert_eq!(2, Solution::largest_combination(vec![8, 8]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user