Skip to content
LC-2044 Medium LeetCode

2044. Count Number of Maximum Bitwise-OR Subsets

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 88% Topics: Array, Backtracking, Bit Manipulation, Enumeration
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(min(2^n, m * n)), m is the 'bitwise or' of nums
# Space: O(min(2^n, m))

import collections


class Solution(object):
    def countMaxOrSubsets(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dp = collections.Counter([0])
        for x in nums:
            for k, v in dp.items():
                dp[k|x] += v
        return dp[reduce(lambda x, y: x|y, nums)]

Solution from kamyu104/LeetCode-Solutions · MIT