Skip to content

Commit 6d8f5af

Browse files
committed
Sync LeetCode submission Runtime - 524 ms (52.94%), Memory - 26.8 MB (58.97%)
1 parent c80fd6a commit 6d8f5af

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>The <strong>bitwise AND</strong> of an array <code>nums</code> is the bitwise AND of all integers in <code>nums</code>.</p>
2+
3+
<ul>
4+
<li>For example, for <code>nums = [1, 5, 3]</code>, the bitwise AND is equal to <code>1 &amp; 5 &amp; 3 = 1</code>.</li>
5+
<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li>
6+
</ul>
7+
8+
<p>You are given an array of positive integers <code>candidates</code>. Evaluate the <strong>bitwise AND</strong> of every <strong>combination</strong> of numbers of <code>candidates</code>. Each number in <code>candidates</code> may only be used <strong>once</strong> in each combination.</p>
9+
10+
<p>Return <em>the size of the <strong>largest</strong> combination of </em><code>candidates</code><em> with a bitwise AND <strong>greater</strong> than </em><code>0</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> candidates = [16,17,71,62,12,24,14]
17+
<strong>Output:</strong> 4
18+
<strong>Explanation:</strong> The combination [16,17,62,24] has a bitwise AND of 16 &amp; 17 &amp; 62 &amp; 24 = 16 &gt; 0.
19+
The size of the combination is 4.
20+
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
21+
Note that more than one combination may have the largest size.
22+
For example, the combination [62,12,24,14] has a bitwise AND of 62 &amp; 12 &amp; 24 &amp; 14 = 8 &gt; 0.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> candidates = [8,8]
29+
<strong>Output:</strong> 2
30+
<strong>Explanation:</strong> The largest combination [8,8] has a bitwise AND of 8 &amp; 8 = 8 &gt; 0.
31+
The size of the combination is 2, so we return 2.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= candidates.length &lt;= 10<sup>5</sup></code></li>
39+
<li><code>1 &lt;= candidates[i] &lt;= 10<sup>7</sup></code></li>
40+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach 1: Using a Bit Count Array
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def largestCombination(self, candidates: List[int]) -> int:
8+
bit_count = [0] * 24
9+
10+
for i in range(24):
11+
for num in candidates:
12+
# check if the i-th bit is set
13+
if (num & (1 << i)) != 0:
14+
bit_count[i] += 1
15+
16+
return max(bit_count)
17+

0 commit comments

Comments
 (0)