|
| 1 | +// 2275. Largest Combination With Bitwise AND Greater Than Zero |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/ |
| 5 | +// |
| 6 | +// Tags: Array - Hash Table - Bit Manipulation - Counting |
| 7 | + |
| 8 | +struct Solution; |
| 9 | +impl Solution { |
| 10 | + /// Iterate over the candidates keeping track of which bits are set using an array to keep |
| 11 | + /// count of how many candidates have the bit in each of the 24 possible possitons set, we |
| 12 | + /// will be able to AND these values. |
| 13 | + /// |
| 14 | + /// Time complexity: O(m*n) - Where m is the number of candidates and n is the log2 of the |
| 15 | + /// largest candidate, since it is limited to a max of 24, we can consider the total O(m). |
| 16 | + /// Space complexity: O(1) - We use an array of size 24. |
| 17 | + /// |
| 18 | + /// Runtime 24 ms Beats 100% |
| 19 | + /// Memory 3.52 MB Beats 9% |
| 20 | + #[allow(dead_code)] |
| 21 | + pub fn largest_combination_loop(candidates: Vec<i32>) -> i32 { |
| 22 | + // We get the maximum number of bits from the problem constrains. <= 10^7 |
| 23 | + // let m = 10000000usize.ilog2() as usize + 1; |
| 24 | + // Use a fixed 24 value to be able to use an array instead of a vector. |
| 25 | + let mut bit_set_count = vec![0i32; 24]; |
| 26 | + for candidate in candidates { |
| 27 | + for i in 0..24 { |
| 28 | + if candidate & 1 << i > 0 { |
| 29 | + bit_set_count[i] += 1; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + bit_set_count.into_iter().max().unwrap() |
| 34 | + } |
| 35 | + |
| 36 | + /// Same solution using an iterator. |
| 37 | + /// |
| 38 | + /// Runtime 22 ms Beats 100% |
| 39 | + /// Memory 3.52 MB Beats 9% |
| 40 | + pub fn largest_combination(candidates: Vec<i32>) -> i32 { |
| 41 | + candidates |
| 42 | + .into_iter() |
| 43 | + .fold([0i32; 24], |mut acc, c| { |
| 44 | + for i in 0..24 { |
| 45 | + if c & 1 << i > 0 { |
| 46 | + acc[i] += 1; |
| 47 | + } |
| 48 | + } |
| 49 | + acc |
| 50 | + }) |
| 51 | + .into_iter() |
| 52 | + .max() |
| 53 | + .unwrap() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Tests. |
| 58 | +fn main() { |
| 59 | + let tests = [(vec![16, 17, 71, 62, 12, 24, 14], 4), (vec![8, 8], 2)]; |
| 60 | + println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len()); |
| 61 | + let mut success = 0; |
| 62 | + for (i, t) in tests.iter().enumerate() { |
| 63 | + let res = Solution::largest_combination(t.0.clone()); |
| 64 | + if res == t.1 { |
| 65 | + success += 1; |
| 66 | + println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i); |
| 67 | + } else { |
| 68 | + println!( |
| 69 | + "\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m", |
| 70 | + i, t.1, res |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + println!(); |
| 75 | + if success == tests.len() { |
| 76 | + println!("\x1b[30;42m✔ All tests passed!\x1b[0m") |
| 77 | + } else if success == 0 { |
| 78 | + println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m") |
| 79 | + } else { |
| 80 | + println!( |
| 81 | + "\x1b[31mx\x1b[95m {} tests failed!\x1b[0m", |
| 82 | + tests.len() - success |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
0 commit comments