-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 524 ms (52.94%), Memory - 26.8 MB …
…(58.97%)
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
2356-largest-combination-with-bitwise-and-greater-than-zero/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<p>The <strong>bitwise AND</strong> of an array <code>nums</code> is the bitwise AND of all integers in <code>nums</code>.</p> | ||
|
||
<ul> | ||
<li>For example, for <code>nums = [1, 5, 3]</code>, the bitwise AND is equal to <code>1 & 5 & 3 = 1</code>.</li> | ||
<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li> | ||
</ul> | ||
|
||
<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> | ||
|
||
<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> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> candidates = [16,17,71,62,12,24,14] | ||
<strong>Output:</strong> 4 | ||
<strong>Explanation:</strong> The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0. | ||
The size of the combination is 4. | ||
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0. | ||
Note that more than one combination may have the largest size. | ||
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> candidates = [8,8] | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0. | ||
The size of the combination is 2, so we return 2. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= candidates.length <= 10<sup>5</sup></code></li> | ||
<li><code>1 <= candidates[i] <= 10<sup>7</sup></code></li> | ||
</ul> |
17 changes: 17 additions & 0 deletions
17
2356-largest-combination-with-bitwise-and-greater-than-zero/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Approach 1: Using a Bit Count Array | ||
|
||
# Time: O(n) | ||
# Space: O(1) | ||
|
||
class Solution: | ||
def largestCombination(self, candidates: List[int]) -> int: | ||
bit_count = [0] * 24 | ||
|
||
for i in range(24): | ||
for num in candidates: | ||
# check if the i-th bit is set | ||
if (num & (1 << i)) != 0: | ||
bit_count[i] += 1 | ||
|
||
return max(bit_count) | ||
|