Skip to content

Commit e2f7c48

Browse files
committed
feat: add solutions to lc problem: No.2044
No.2044.Count Number of Maximum Bitwise-OR Subsets
1 parent 1455c9a commit e2f7c48

File tree

17 files changed

+385
-628
lines changed

17 files changed

+385
-628
lines changed

solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md

Lines changed: 126 additions & 214 deletions
Large diffs are not rendered by default.

solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md

Lines changed: 130 additions & 212 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
class Solution {
22
public:
3-
int mx;
4-
int ans;
5-
vector<int> nums;
6-
73
int countMaxOrSubsets(vector<int>& nums) {
8-
this->nums = nums;
9-
mx = 0;
10-
ans = 0;
11-
for (int x : nums) mx |= x;
4+
int ans = 0;
5+
int mx = accumulate(nums.begin(), nums.end(), 0, bit_or<int>());
6+
auto dfs = [&](this auto&& dfs, int i, int t) {
7+
if (i == nums.size()) {
8+
if (t == mx) {
9+
ans++;
10+
}
11+
return;
12+
}
13+
dfs(i + 1, t);
14+
dfs(i + 1, t | nums[i]);
15+
};
1216
dfs(0, 0);
1317
return ans;
1418
}
15-
16-
void dfs(int i, int t) {
17-
if (i == nums.size()) {
18-
if (t == mx) ++ans;
19-
return;
20-
}
21-
dfs(i + 1, t);
22-
dfs(i + 1, t | nums[i]);
23-
}
24-
};
19+
};

solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
func countMaxOrSubsets(nums []int) int {
2-
mx, ans := 0, 0
1+
func countMaxOrSubsets(nums []int) (ans int) {
2+
mx := 0
33
for _, x := range nums {
44
mx |= x
55
}
@@ -17,5 +17,5 @@ func countMaxOrSubsets(nums []int) int {
1717
}
1818

1919
dfs(0, 0)
20-
return ans
20+
return
2121
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution:
22
def countMaxOrSubsets(self, nums: List[int]) -> int:
3-
mx = ans = 0
4-
for x in nums:
5-
mx |= x
6-
73
def dfs(i, t):
8-
nonlocal mx, ans
4+
nonlocal ans, mx
95
if i == len(nums):
106
if t == mx:
117
ans += 1
128
return
139
dfs(i + 1, t)
1410
dfs(i + 1, t | nums[i])
1511

12+
ans = 0
13+
mx = reduce(lambda x, y: x | y, nums)
1614
dfs(0, 0)
1715
return ans
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
impl Solution {
2-
fn dfs(nums: &Vec<i32>, i: usize, sum: i32) -> (i32, i32) {
3-
let n = nums.len();
4-
let mut max = i32::MIN;
5-
let mut res = 0;
6-
for j in i..n {
7-
let num = sum | nums[j];
8-
if num >= max {
9-
if num > max {
10-
max = num;
11-
res = 0;
12-
}
13-
res += 1;
14-
}
15-
let (r_max, r_res) = Self::dfs(nums, j + 1, num);
16-
if r_max >= max {
17-
if r_max > max {
18-
max = r_max;
19-
res = 0;
2+
pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
3+
let mut ans = 0;
4+
let mx = nums.iter().fold(0, |x, &y| x | y);
5+
6+
fn dfs(i: usize, t: i32, nums: &Vec<i32>, mx: i32, ans: &mut i32) {
7+
if i == nums.len() {
8+
if t == mx {
9+
*ans += 1;
2010
}
21-
res += r_res;
11+
return;
2212
}
13+
dfs(i + 1, t, nums, mx, ans);
14+
dfs(i + 1, t | nums[i], nums, mx, ans);
2315
}
24-
(max, res)
25-
}
2616

27-
pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
28-
Self::dfs(&nums, 0, 0).1
17+
dfs(0, 0, &nums, mx, &mut ans);
18+
ans
2919
}
3020
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
function countMaxOrSubsets(nums: number[]): number {
2-
let n = nums.length;
3-
let max = 0;
4-
for (let i = 0; i < n; i++) {
5-
max |= nums[i];
6-
}
72
let ans = 0;
8-
function dfs(pre: number, depth: number): void {
9-
if (depth == n) {
10-
if (pre == max) ++ans;
3+
const mx = nums.reduce((x, y) => x | y, 0);
4+
5+
const dfs = (i: number, t: number) => {
6+
if (i === nums.length) {
7+
if (t === mx) {
8+
ans++;
9+
}
1110
return;
1211
}
13-
dfs(pre, depth + 1);
14-
dfs(pre | nums[depth], depth + 1);
15-
}
12+
dfs(i + 1, t);
13+
dfs(i + 1, t | nums[i]);
14+
};
15+
1616
dfs(0, 0);
1717
return ans;
1818
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
class Solution {
22
public:
3-
int mx;
4-
int ans;
5-
63
int countMaxOrSubsets(vector<int>& nums) {
7-
dfs(0, 0, nums);
8-
return ans;
9-
}
10-
11-
void dfs(int u, int t, vector<int>& nums) {
12-
if (u == nums.size()) {
13-
if (t > mx) {
4+
int n = nums.size();
5+
int ans = 0;
6+
int mx = 0;
7+
for (int mask = 1; mask < 1 << n; ++mask) {
8+
int t = 0;
9+
for (int i = 0; i < n; ++i) {
10+
if ((mask >> i) & 1) {
11+
t |= nums[i];
12+
}
13+
}
14+
if (mx < t) {
1415
mx = t;
1516
ans = 1;
16-
} else if (t == mx)
17+
} else if (mx == t)
1718
++ans;
18-
return;
1919
}
20-
dfs(u + 1, t, nums);
21-
dfs(u + 1, t | nums[u], nums);
20+
return ans;
2221
}
2322
};
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
func countMaxOrSubsets(nums []int) int {
1+
func countMaxOrSubsets(nums []int) (ans int) {
22
n := len(nums)
3-
ans := 0
43
mx := 0
5-
for mask := 1; mask < 1<<n; mask++ {
4+
5+
for mask := 0; mask < (1 << n); mask++ {
66
t := 0
77
for i, v := range nums {
8-
if ((mask >> i) & 1) == 1 {
8+
if (mask>>i)&1 == 1 {
99
t |= v
1010
}
1111
}
@@ -16,5 +16,6 @@ func countMaxOrSubsets(nums []int) int {
1616
ans++
1717
}
1818
}
19-
return ans
20-
}
19+
20+
return
21+
}
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
class Solution {
2-
private int mx;
3-
private int ans;
4-
private int[] nums;
5-
62
public int countMaxOrSubsets(int[] nums) {
7-
this.nums = nums;
8-
dfs(0, 0);
9-
return ans;
10-
}
11-
12-
private void dfs(int u, int t) {
13-
if (u == nums.length) {
14-
if (t > mx) {
3+
int n = nums.length;
4+
int ans = 0;
5+
int mx = 0;
6+
for (int mask = 1; mask < 1 << n; ++mask) {
7+
int t = 0;
8+
for (int i = 0; i < n; ++i) {
9+
if (((mask >> i) & 1) == 1) {
10+
t |= nums[i];
11+
}
12+
}
13+
if (mx < t) {
1514
mx = t;
1615
ans = 1;
17-
} else if (t == mx) {
16+
} else if (mx == t) {
1817
++ans;
1918
}
20-
return;
2119
}
22-
dfs(u + 1, t);
23-
dfs(u + 1, t | nums[u]);
20+
return ans;
2421
}
2522
}

0 commit comments

Comments
 (0)