From e2f7c482b5ff1dad3caa4c194aca5c5ca0059721 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 28 Jul 2025 07:30:07 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2044 No.2044.Count Number of Maximum Bitwise-OR Subsets --- .../README.md | 340 +++++++---------- .../README_EN.md | 342 +++++++----------- .../Solution.cpp | 31 +- .../Solution.go | 6 +- .../Solution.py | 8 +- .../Solution.rs | 36 +- .../Solution.ts | 22 +- .../Solution2.cpp | 27 +- .../Solution2.go | 13 +- .../Solution2.java | 29 +- .../Solution2.py | 26 +- .../Solution2.rs | 24 ++ .../Solution2.ts | 31 +- .../Solution3.cpp | 22 -- .../Solution3.go | 18 - .../Solution3.java | 22 -- .../Solution3.py | 16 - 17 files changed, 385 insertions(+), 628 deletions(-) create mode 100644 solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.rs delete mode 100644 solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.cpp delete mode 100644 solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.go delete mode 100644 solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.java delete mode 100644 solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.py diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md index e2c3dbe051e3e..25d2759b63984 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md @@ -77,9 +77,15 @@ tags: ### 方法一:DFS -简单 DFS。可以预先算出按位或的最大值 mx,然后 DFS 搜索按位或结果等于 mx 的所有子集数。也可以在 DFS 搜索中逐渐更新 mx 与对应的子集数。 +数组 $\textit{nums}$ 中按位或的最大值 $\textit{mx}$ 可以通过对数组中所有元素按位或得到。 -时间复杂度 $O(2^n)$。 +然后我们可以使用深度优先搜索来枚举所有子集,统计按位或等于 $\textit{mx}$ 的子集个数。我们设计一个函数 $\text{dfs(i, t)}$,表示从下标 $\textit{i}$ 开始,当前按位或的值为 $\textit{t}$ 的子集个数。初始时 $\textit{i} = 0$, $\textit{t} = 0$。 + +在函数 $\text{dfs(i, t)}$ 中,如果 $\textit{i}$ 等于数组长度,说明已经枚举完所有元素,此时如果 $\textit{t}$ 等于 $\textit{mx}$,则答案加一。否则,我们可以选择不包含当前元素 $\textit{nums[i]}$,或者包含当前元素 $\textit{nums[i]}$,因此我们可以递归调用 $\text{dfs(i + 1, t)}$ 和 $\text{dfs(i + 1, t | nums[i])}$。 + +最后返回答案即可。 + +时间复杂度 $O(2^n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 @@ -88,12 +94,8 @@ tags: ```python 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 @@ -101,6 +103,8 @@ class Solution: 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 ``` @@ -141,35 +145,30 @@ class Solution { ```cpp class Solution { public: - int mx; - int ans; - vector nums; - int countMaxOrSubsets(vector& 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()); + 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]); - } }; ``` #### Go ```go -func countMaxOrSubsets(nums []int) int { - mx, ans := 0, 0 +func countMaxOrSubsets(nums []int) (ans int) { + mx := 0 for _, x := range nums { mx |= x } @@ -187,7 +186,7 @@ func countMaxOrSubsets(nums []int) int { } dfs(0, 0) - return ans + return } ``` @@ -195,20 +194,20 @@ func countMaxOrSubsets(nums []int) int { ```ts 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; } @@ -218,33 +217,23 @@ function countMaxOrSubsets(nums: number[]): number { ```rust impl Solution { - fn dfs(nums: &Vec, 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 { + let mut ans = 0; + let mx = nums.iter().fold(0, |x, &y| x | y); + + fn dfs(i: usize, t: i32, nums: &Vec, 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 { - Self::dfs(&nums, 0, 0).1 + dfs(0, 0, &nums, mx, &mut ans); + ans } } ``` @@ -257,147 +246,11 @@ impl Solution { ### 方法二:二进制枚举 -时间复杂度 $O(n*2^n)$。 +我们可以使用二进制枚举来统计所有子集的按位或结果。对于长度为 $n$ 的数组 $\textit{nums}$,我们可以使用一个整数 $\textit{mask}$ 来表示一个子集,其中 $\textit{mask}$ 的第 $i$ 位为 1 表示包含元素 $\textit{nums[i]}$,为 0 则表示不包含。 - +我们可以遍历所有可能的 $\textit{mask}$,从 $0$ 到 $2^n - 1$。对于每个 $\textit{mask}$,我们可以计算出对应子集的按位或结果,并更新最大值 $\textit{mx}$ 和答案 $\textit{ans}$。 -#### Python3 - -```python -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) - return ans -``` - -#### Java - -```java -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) { - mx = t; - ans = 1; - } else if (t == mx) { - ++ans; - } - return; - } - dfs(u + 1, t); - dfs(u + 1, t | nums[u]); - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int mx; - int ans; - - int countMaxOrSubsets(vector& nums) { - dfs(0, 0, nums); - return ans; - } - - void dfs(int u, int t, vector& nums) { - if (u == nums.size()) { - if (t > mx) { - mx = t; - ans = 1; - } else if (t == mx) - ++ans; - return; - } - dfs(u + 1, t, nums); - dfs(u + 1, t | nums[u], nums); - } -}; -``` - -#### Go - -```go -func countMaxOrSubsets(nums []int) int { - n := len(nums) - ans := 0 - mx := 0 - for mask := 1; mask < 1<> i) & 1) == 1 { - t |= v - } - } - if mx < t { - mx = t - ans = 1 - } else if mx == t { - ans++ - } - } - return ans -} -``` - -#### TypeScript - -```ts -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++; - } - dfs(j + 1, num); - } - }; - dfs(0, 0); - - return res; -} -``` - - - - - - - -### 方法三 +时间复杂度 $O(2^n \cdot n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -479,23 +332,82 @@ public: #### Go ```go -func countMaxOrSubsets(nums []int) int { - mx, ans := 0, 0 - var dfs func(u, t int) - dfs = func(u, t int) { - if u == len(nums) { - if t > mx { - mx, ans = t, 1 - } else if t == mx { - ans++ +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 } - return } - dfs(u+1, t) - dfs(u+1, t|nums[u]) + if mx < t { + mx = t + ans = 1 + } else if mx == t { + ans++ + } } - dfs(0, 0) - return ans + + return +} +``` + +#### TypeScript + +```ts +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 + +```rust +impl Solution { + pub fn count_max_or_subsets(nums: Vec) -> 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 + } } ``` diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md index 8f9aa2c0c8fa3..210bb8e736522 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md @@ -73,7 +73,17 @@ tags: -### Solution 1 +### Solution 1: DFS + +The maximum bitwise OR value $\textit{mx}$ in the array $\textit{nums}$ can be obtained by performing bitwise OR on all elements in the array. + +Then we can use depth-first search to enumerate all subsets and count the number of subsets whose bitwise OR equals $\textit{mx}$. We design a function $\text{dfs(i, t)}$, which represents the number of subsets starting from index $\textit{i}$ with the current bitwise OR value being $\textit{t}$. Initially, $\textit{i} = 0$ and $\textit{t} = 0$. + +In the function $\text{dfs(i, t)}$, if $\textit{i}$ equals the array length, it means we have enumerated all elements. At this point, if $\textit{t}$ equals $\textit{mx}$, we increment the answer by one. Otherwise, we can choose to either exclude the current element $\textit{nums[i]}$ or include the current element $\textit{nums[i]}$, so we can recursively call $\text{dfs(i + 1, t)}$ and $\text{dfs(i + 1, t | nums[i])}$. + +Finally, we return the answer. + +The time complexity is $O(2^n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. @@ -82,12 +92,8 @@ tags: ```python 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 @@ -95,6 +101,8 @@ class Solution: 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 ``` @@ -135,35 +143,30 @@ class Solution { ```cpp class Solution { public: - int mx; - int ans; - vector nums; - int countMaxOrSubsets(vector& 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()); + 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]); - } }; ``` #### Go ```go -func countMaxOrSubsets(nums []int) int { - mx, ans := 0, 0 +func countMaxOrSubsets(nums []int) (ans int) { + mx := 0 for _, x := range nums { mx |= x } @@ -181,7 +184,7 @@ func countMaxOrSubsets(nums []int) int { } dfs(0, 0) - return ans + return } ``` @@ -189,20 +192,20 @@ func countMaxOrSubsets(nums []int) int { ```ts 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; } @@ -212,33 +215,23 @@ function countMaxOrSubsets(nums: number[]): number { ```rust impl Solution { - fn dfs(nums: &Vec, 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 { + let mut ans = 0; + let mx = nums.iter().fold(0, |x, &y| x | y); + + fn dfs(i: usize, t: i32, nums: &Vec, 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 { - Self::dfs(&nums, 0, 0).1 + dfs(0, 0, &nums, mx, &mut ans); + ans } } ``` @@ -249,147 +242,13 @@ impl Solution { -### Solution 2 - - - -#### Python3 - -```python -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) - return ans -``` - -#### Java - -```java -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) { - mx = t; - ans = 1; - } else if (t == mx) { - ++ans; - } - return; - } - dfs(u + 1, t); - dfs(u + 1, t | nums[u]); - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int mx; - int ans; - - int countMaxOrSubsets(vector& nums) { - dfs(0, 0, nums); - return ans; - } - - void dfs(int u, int t, vector& nums) { - if (u == nums.size()) { - if (t > mx) { - mx = t; - ans = 1; - } else if (t == mx) - ++ans; - return; - } - dfs(u + 1, t, nums); - dfs(u + 1, t | nums[u], nums); - } -}; -``` - -#### Go - -```go -func countMaxOrSubsets(nums []int) int { - n := len(nums) - ans := 0 - mx := 0 - for mask := 1; mask < 1<> i) & 1) == 1 { - t |= v - } - } - if mx < t { - mx = t - ans = 1 - } else if mx == t { - ans++ - } - } - return ans -} -``` - -#### TypeScript +### Solution 2: Binary Enumeration -```ts -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++; - } - dfs(j + 1, num); - } - }; - dfs(0, 0); +We can use binary enumeration to count the bitwise OR results of all subsets. For an array $\textit{nums}$ of length $n$, we can use an integer $\textit{mask}$ to represent a subset, where the $i$-th bit of $\textit{mask}$ being 1 means including element $\textit{nums[i]}$, and 0 means not including it. - return res; -} -``` +We can iterate through all possible $\textit{mask}$ values from $0$ to $2^n - 1$. For each $\textit{mask}$, we can calculate the bitwise OR result of the corresponding subset and update the maximum value $\textit{mx}$ and answer $\textit{ans}$. - - - - - - -### Solution 3 +The time complexity is $O(2^n \cdot n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -471,23 +330,82 @@ public: #### Go ```go -func countMaxOrSubsets(nums []int) int { - mx, ans := 0, 0 - var dfs func(u, t int) - dfs = func(u, t int) { - if u == len(nums) { - if t > mx { - mx, ans = t, 1 - } else if t == mx { - ans++ +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 } - return } - dfs(u+1, t) - dfs(u+1, t|nums[u]) + if mx < t { + mx = t + ans = 1 + } else if mx == t { + ans++ + } } - dfs(0, 0) - return ans + + return +} +``` + +#### TypeScript + +```ts +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 + +```rust +impl Solution { + pub fn count_max_or_subsets(nums: Vec) -> 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 + } } ``` diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp index c8939eb26024f..4e32478c50149 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp @@ -1,24 +1,19 @@ class Solution { public: - int mx; - int ans; - vector nums; - int countMaxOrSubsets(vector& 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()); + 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]); - } -}; \ No newline at end of file +}; diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.go b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.go index 19cc168adee93..88813db6ec65e 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.go +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.go @@ -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 } @@ -17,5 +17,5 @@ func countMaxOrSubsets(nums []int) int { } dfs(0, 0) - return ans + return } \ No newline at end of file diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.py b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.py index 334d606fccb6b..c8718ffec466c 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.py +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.py @@ -1,11 +1,7 @@ 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 @@ -13,5 +9,7 @@ def dfs(i, t): 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 diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.rs b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.rs index 9132d7dd6b653..4362bfdb0cdd8 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.rs +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.rs @@ -1,30 +1,20 @@ impl Solution { - fn dfs(nums: &Vec, 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 { + let mut ans = 0; + let mx = nums.iter().fold(0, |x, &y| x | y); + + fn dfs(i: usize, t: i32, nums: &Vec, 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 { - Self::dfs(&nums, 0, 0).1 + dfs(0, 0, &nums, mx, &mut ans); + ans } } diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts index 7892d8fa13ba1..fca84d99fb8d7 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts @@ -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; } diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.cpp b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.cpp index c8e8122f8587b..aebbbcaebe217 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.cpp +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.cpp @@ -1,23 +1,22 @@ class Solution { public: - int mx; - int ans; - int countMaxOrSubsets(vector& nums) { - dfs(0, 0, nums); - return ans; - } - - void dfs(int u, int t, vector& 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; } }; \ No newline at end of file diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.go b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.go index d017ea1ba963b..ecd53632e8a91 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.go +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.go @@ -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<> i) & 1) == 1 { + if (mask>>i)&1 == 1 { t |= v } } @@ -16,5 +16,6 @@ func countMaxOrSubsets(nums []int) int { ans++ } } - return ans -} \ No newline at end of file + + return +} diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.java b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.java index 75143a8b30763..401802a01543c 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.java +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.java @@ -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; } } \ No newline at end of file diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.py b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.py index c7fb372d8f204..7f6b040cf392b 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.py +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.py @@ -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 diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.rs b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.rs new file mode 100644 index 0000000000000..d924bfcf3befa --- /dev/null +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.rs @@ -0,0 +1,24 @@ +impl Solution { + pub fn count_max_or_subsets(nums: Vec) -> 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 + } +} diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.ts b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.ts index e3c35b2161906..3da88b9c02132 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.ts +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution2.ts @@ -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; } diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.cpp b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.cpp deleted file mode 100644 index aebbbcaebe217..0000000000000 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.cpp +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -public: - int countMaxOrSubsets(vector& 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; - } -}; \ No newline at end of file diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.go b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.go deleted file mode 100644 index 0047a1dbf7d07..0000000000000 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.go +++ /dev/null @@ -1,18 +0,0 @@ -func countMaxOrSubsets(nums []int) int { - mx, ans := 0, 0 - var dfs func(u, t int) - dfs = func(u, t int) { - if u == len(nums) { - if t > mx { - mx, ans = t, 1 - } else if t == mx { - ans++ - } - return - } - dfs(u+1, t) - dfs(u+1, t|nums[u]) - } - dfs(0, 0) - return ans -} \ No newline at end of file diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.java b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.java deleted file mode 100644 index 401802a01543c..0000000000000 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.java +++ /dev/null @@ -1,22 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.py b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.py deleted file mode 100644 index 7f6b040cf392b..0000000000000 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution3.py +++ /dev/null @@ -1,16 +0,0 @@ -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