Skip to content

Commit 2957dca

Browse files
committed
LC 2275. Largest Combination With Bitwise AND Greater Than Zero (Rust)
1 parent 2934733 commit 2957dca

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ to the solution in this repository.
683683
| [2251. Number of Flowers in Full Bloom][lc2251] | 🔴 Hard | [![rust](res/rs.png)][lc2251rs] |
684684
| [2256. Minimum Average Difference][lc2256] | 🟠 Medium | [![python](res/py.png)][lc2256py] |
685685
| [2265. Count Nodes Equal to Average of Subtree][lc2265] | 🟠 Medium | [![rust](res/rs.png)][lc2265rs] |
686+
| [2275. Largest Combination With Bitwise AND Greater Than Zero][lc2275] | 🟠 Medium | [![rust](res/rs.png)][lc2275rs] |
686687
| [2279. Maximum Bags With Full Capacity of Rocks][lc2279] | 🟠 Medium | [![python](res/py.png)][lc2279py] |
687688
| [2285. Maximum Total Importance of Roads][lc2285] | 🟠 Medium | [![rust](res/rs.png)][lc2285rs] |
688689
| [2300. Successful Pairs of Spells and Potions][lc2300] | 🟠 Medium | [![python](res/py.png)][lc2300py] [![rust](res/rs.png)][lc2300rs] |
@@ -2286,6 +2287,8 @@ to the solution in this repository.
22862287
[lc2256py]: leetcode/minimum-average-difference.py
22872288
[lc2265]: https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/
22882289
[lc2265rs]: leetcode/count-nodes-equal-to-average-of-subtree.rs
2290+
[lc2275]: https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/
2291+
[lc2275rs]: leetcode/largest-combination-with-bitwise-and-greater-than-zero.rs
22892292
[lc2279]: https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/
22902293
[lc2279py]: leetcode/maximum-bags-with-full-capacity-of-rocks.py
22912294
[lc2285]: https://leetcode.com/problems/maximum-total-importance-of-roads/
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)