2044. 统计按位或能得到最大值的子集数目
July 27, 2025 · View on GitHub
题目描述
给你一个整数数组 nums ,请你找出 nums 子集 按位或 可能得到的 最大值 ,并返回按位或能得到最大值的 不同非空子集的数目 。
如果数组 a 可以由数组 b 删除一些元素(或不删除)得到,则认为数组 a 是数组 b 的一个 子集 。如果选中的元素下标位置不一样,则认为两个子集 不同 。
对数组 a 执行 按位或 ,结果等于 a[0] OR a[1] OR ... OR a[a.length - 1](下标从 0 开始)。
示例 1:
输入:nums = [3,1] 输出:2 解释:子集按位或能得到的最大值是 3 。有 2 个子集按位或可以得到 3 : - [3] - [3,1]
示例 2:
输入:nums = [2,2,2] 输出:7 解释:[2,2,2] 的所有非空子集的按位或都可以得到 2 。总共有 23 - 1 = 7 个子集。
示例 3:
输入:nums = [3,2,1,5] 输出:6 解释:子集按位或可能的最大值是 7 。有 6 个子集按位或可以得到 7 : - [3,5] - [3,1,5] - [3,2,5] - [3,2,1,5] - [2,5] - [2,1,5]
提示:
1 <= nums.length <= 161 <= nums[i] <= 105
解法
方法一:DFS
数组 中按位或的最大值 可以通过对数组中所有元素按位或得到。
然后我们可以使用深度优先搜索来枚举所有子集,统计按位或等于 的子集个数。我们设计一个函数 ,表示从下标 开始,当前按位或的值为 的子集个数。初始时 , 。
在函数 中,如果 等于数组长度,说明已经枚举完所有元素,此时如果 等于 ,则答案加一。否则,我们可以选择不包含当前元素 ,或者包含当前元素 ,因此我们可以递归调用 和 。
最后返回答案即可。
时间复杂度 ,空间复杂度 。其中 是数组 的长度。
Python3
class Solution:
def countMaxOrSubsets(self, nums: List[int]) -> int:
def dfs(i, t):
nonlocal ans, mx
if i == len(nums):
if t == mx:
ans += 1
return
dfs(i + 1, t)
dfs(i + 1, t | nums[i])
ans = 0
mx = reduce(lambda x, y: x | y, nums)
dfs(0, 0)
return ans
Java
class Solution {
private int mx;
private int ans;
private int[] nums;
public int countMaxOrSubsets(int[] nums) {
mx = 0;
for (int x : nums) {
mx |= x;
}
this.nums = nums;
dfs(0, 0);
return ans;
}
private void dfs(int i, int t) {
if (i == nums.length) {
if (t == mx) {
++ans;
}
return;
}
dfs(i + 1, t);
dfs(i + 1, t | nums[i]);
}
}
C++
class Solution {
public:
int countMaxOrSubsets(vector<int>& nums) {
int ans = 0;
int mx = accumulate(nums.begin(), nums.end(), 0, bit_or<int>());
auto dfs = [&](this auto&& dfs, int i, int t) {
if (i == nums.size()) {
if (t == mx) {
ans++;
}
return;
}
dfs(i + 1, t);
dfs(i + 1, t | nums[i]);
};
dfs(0, 0);
return ans;
}
};
Go
func countMaxOrSubsets(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx |= x
}
var dfs func(i, t int)
dfs = func(i, t int) {
if i == len(nums) {
if t == mx {
ans++
}
return
}
dfs(i+1, t)
dfs(i+1, t|nums[i])
}
dfs(0, 0)
return
}
TypeScript
function countMaxOrSubsets(nums: number[]): number {
let ans = 0;
const mx = nums.reduce((x, y) => x | y, 0);
const dfs = (i: number, t: number) => {
if (i === nums.length) {
if (t === mx) {
ans++;
}
return;
}
dfs(i + 1, t);
dfs(i + 1, t | nums[i]);
};
dfs(0, 0);
return ans;
}
Rust
impl Solution {
pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
let mut ans = 0;
let mx = nums.iter().fold(0, |x, &y| x | y);
fn dfs(i: usize, t: i32, nums: &Vec<i32>, mx: i32, ans: &mut i32) {
if i == nums.len() {
if t == mx {
*ans += 1;
}
return;
}
dfs(i + 1, t, nums, mx, ans);
dfs(i + 1, t | nums[i], nums, mx, ans);
}
dfs(0, 0, &nums, mx, &mut ans);
ans
}
}
方法二:二进制枚举
我们可以使用二进制枚举来统计所有子集的按位或结果。对于长度为 的数组 ,我们可以使用一个整数 来表示一个子集,其中 的第 位为 1 表示包含元素 ,为 0 则表示不包含。
我们可以遍历所有可能的 ,从 $0 到 \2^n - 1\textit{mask}\textit{mx}\textit{ans}$。
时间复杂度 ,其中 是数组 的长度。空间复杂度 。
Python3
class Solution:
def countMaxOrSubsets(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
mx = 0
for mask in range(1 << n):
t = 0
for i, v in enumerate(nums):
if (mask >> i) & 1:
t |= v
if mx < t:
mx = t
ans = 1
elif mx == t:
ans += 1
return ans
Java
class Solution {
public int countMaxOrSubsets(int[] nums) {
int n = nums.length;
int ans = 0;
int mx = 0;
for (int mask = 1; mask < 1 << n; ++mask) {
int t = 0;
for (int i = 0; i < n; ++i) {
if (((mask >> i) & 1) == 1) {
t |= nums[i];
}
}
if (mx < t) {
mx = t;
ans = 1;
} else if (mx == t) {
++ans;
}
}
return ans;
}
}
C++
class Solution {
public:
int countMaxOrSubsets(vector<int>& nums) {
int n = nums.size();
int ans = 0;
int mx = 0;
for (int mask = 1; mask < 1 << n; ++mask) {
int t = 0;
for (int i = 0; i < n; ++i) {
if ((mask >> i) & 1) {
t |= nums[i];
}
}
if (mx < t) {
mx = t;
ans = 1;
} else if (mx == t)
++ans;
}
return ans;
}
};
Go
func countMaxOrSubsets(nums []int) (ans int) {
n := len(nums)
mx := 0
for mask := 0; mask < (1 << n); mask++ {
t := 0
for i, v := range nums {
if (mask>>i)&1 == 1 {
t |= v
}
}
if mx < t {
mx = t
ans = 1
} else if mx == t {
ans++
}
}
return
}
TypeScript
function countMaxOrSubsets(nums: number[]): number {
const n = nums.length;
let ans = 0;
let mx = 0;
for (let mask = 0; mask < 1 << n; mask++) {
let t = 0;
for (let i = 0; i < n; i++) {
if ((mask >> i) & 1) {
t |= nums[i];
}
}
if (mx < t) {
mx = t;
ans = 1;
} else if (mx === t) {
ans++;
}
}
return ans;
}
Rust
impl Solution {
pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut ans = 0;
let mut mx = 0;
for mask in 0..(1 << n) {
let mut t = 0;
for i in 0..n {
if (mask >> i) & 1 == 1 {
t |= nums[i];
}
}
if mx < t {
mx = t;
ans = 1;
} else if mx == t {
ans += 1;
}
}
ans
}
}