Skip to content

Commit 246d7cd

Browse files
committed
Sync LeetCode submission Runtime - 235 ms (68.11%), Memory - 17.7 MB (74.49%)
1 parent 36d69ce commit 246d7cd

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>Given an integer array <code>nums</code>, find the <strong>maximum</strong> possible <strong>bitwise OR</strong> of a subset of <code>nums</code> and return <em>the <strong>number of different non-empty subsets</strong> with the maximum bitwise OR</em>.</p>
2+
3+
<p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>. Two subsets are considered <strong>different</strong> if the indices of the elements chosen are different.</p>
4+
5+
<p>The bitwise OR of an array <code>a</code> is equal to <code>a[0] <strong>OR</strong> a[1] <strong>OR</strong> ... <strong>OR</strong> a[a.length - 1]</code> (<strong>0-indexed</strong>).</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [3,1]
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong> The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
14+
- [3]
15+
- [3,1]
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [2,2,2]
22+
<strong>Output:</strong> 7
23+
<strong>Explanation:</strong> All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 2<sup>3</sup> - 1 = 7 total subsets.
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [3,2,1,5]
30+
<strong>Output:</strong> 6
31+
<strong>Explanation:</strong> The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:
32+
- [3,5]
33+
- [3,1,5]
34+
- [3,2,5]
35+
- [3,2,1,5]
36+
- [2,5]
37+
- [2,1,5]</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums.length &lt;= 16</code></li>
44+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
45+
</ul>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach 1: Recursion
2+
3+
# Time: O(2^n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def countMaxOrSubsets(self, nums: List[int]) -> int:
8+
max_or_value = 0
9+
for num in nums:
10+
max_or_value |= num
11+
return self._count_subsets(nums, 0, 0, max_or_value)
12+
13+
def _count_subsets(self, nums, index, current_or, target_or):
14+
# Base case: reached end of array
15+
if index == len(nums):
16+
return 1 if current_or == target_or else 0
17+
18+
# Don't include current number
19+
count_without = self._count_subsets(nums, index + 1, current_or, target_or)
20+
21+
# Include current number
22+
count_with = self._count_subsets(nums, index + 1, current_or | nums[index], target_or)
23+
24+
# Return sum of both cases
25+
return count_without + count_with
26+

0 commit comments

Comments
 (0)