Skip to content

feat: add solutions to lc problem: No.2044 #4602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
class Solution {
public:
int mx;
int ans;
vector<int> nums;

int countMaxOrSubsets(vector<int>& nums) {
this->nums = nums;
mx = 0;
ans = 0;
for (int x : nums) mx |= x;
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;
}

void dfs(int i, int t) {
if (i == nums.size()) {
if (t == mx) ++ans;
return;
}
dfs(i + 1, t);
dfs(i + 1, t | nums[i]);
}
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func countMaxOrSubsets(nums []int) int {
mx, ans := 0, 0
func countMaxOrSubsets(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx |= x
}
Expand All @@ -17,5 +17,5 @@ func countMaxOrSubsets(nums []int) int {
}

dfs(0, 0)
return ans
return
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class Solution:
def countMaxOrSubsets(self, nums: List[int]) -> int:
mx = ans = 0
for x in nums:
mx |= x

def dfs(i, t):
nonlocal mx, ans
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
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
impl Solution {
fn dfs(nums: &Vec<i32>, i: usize, sum: i32) -> (i32, i32) {
let n = nums.len();
let mut max = i32::MIN;
let mut res = 0;
for j in i..n {
let num = sum | nums[j];
if num >= max {
if num > max {
max = num;
res = 0;
}
res += 1;
}
let (r_max, r_res) = Self::dfs(nums, j + 1, num);
if r_max >= max {
if r_max > max {
max = r_max;
res = 0;
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;
}
res += r_res;
return;
}
dfs(i + 1, t, nums, mx, ans);
dfs(i + 1, t | nums[i], nums, mx, ans);
}
(max, res)
}

pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
Self::dfs(&nums, 0, 0).1
dfs(0, 0, &nums, mx, &mut ans);
ans
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function countMaxOrSubsets(nums: number[]): number {
let n = nums.length;
let max = 0;
for (let i = 0; i < n; i++) {
max |= nums[i];
}
let ans = 0;
function dfs(pre: number, depth: number): void {
if (depth == n) {
if (pre == max) ++ans;
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(pre, depth + 1);
dfs(pre | nums[depth], depth + 1);
}
dfs(i + 1, t);
dfs(i + 1, t | nums[i]);
};

dfs(0, 0);
return ans;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
class Solution {
public:
int mx;
int ans;

int countMaxOrSubsets(vector<int>& nums) {
dfs(0, 0, nums);
return ans;
}

void dfs(int u, int t, vector<int>& nums) {
if (u == nums.size()) {
if (t > mx) {
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 (t == mx)
} else if (mx == t)
++ans;
return;
}
dfs(u + 1, t, nums);
dfs(u + 1, t | nums[u], nums);
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
func countMaxOrSubsets(nums []int) int {
func countMaxOrSubsets(nums []int) (ans int) {
n := len(nums)
ans := 0
mx := 0
for mask := 1; mask < 1<<n; mask++ {

for mask := 0; mask < (1 << n); mask++ {
t := 0
for i, v := range nums {
if ((mask >> i) & 1) == 1 {
if (mask>>i)&1 == 1 {
t |= v
}
}
Expand All @@ -16,5 +16,6 @@ func countMaxOrSubsets(nums []int) int {
ans++
}
}
return ans
}

return
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
class Solution {
private int mx;
private int ans;
private int[] nums;

public int countMaxOrSubsets(int[] nums) {
this.nums = nums;
dfs(0, 0);
return ans;
}

private void dfs(int u, int t) {
if (u == nums.length) {
if (t > mx) {
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 (t == mx) {
} else if (mx == t) {
++ans;
}
return;
}
dfs(u + 1, t);
dfs(u + 1, t | nums[u]);
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Solution:
def countMaxOrSubsets(self, nums: List[int]) -> int:
def dfs(u, t):
nonlocal ans, mx
if u == len(nums):
if t > mx:
mx, ans = t, 1
elif t == mx:
ans += 1
return
dfs(u + 1, t | nums[u])
dfs(u + 1, t)

ans = mx = 0
dfs(0, 0)
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
function countMaxOrSubsets(nums: number[]): number {
const n = nums.length;
let res = 0;
let max = -Infinity;
const dfs = (i: number, sum: number) => {
for (let j = i; j < n; j++) {
const num = sum | nums[j];
if (num >= max) {
if (num > max) {
max = num;
res = 0;
}
res++;
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];
}
dfs(j + 1, num);
}
};
dfs(0, 0);
if (mx < t) {
mx = t;
ans = 1;
} else if (mx === t) {
ans++;
}
}

return res;
return ans;
}

This file was deleted.

This file was deleted.

Loading