Skip to content

Commit 0190cfd

Browse files
authored
Create Count Number of Maximum Bitwise-OR Subsets.java
1 parent 45105d8 commit 0190cfd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int countMaxOrSubsets(int[] nums) {
3+
int n = nums.length;
4+
int maxValue = 0;
5+
for (int num : nums) {
6+
maxValue |= num;
7+
}
8+
Integer[][] dp = new Integer[n][maxValue + 1];
9+
return countMaxOrSubsets(nums, 0, 0, maxValue, dp);
10+
}
11+
12+
private int countMaxOrSubsets(int[] nums, int idx, int current, int maxValue, Integer[][] dp) {
13+
if (idx == nums.length) {
14+
return (current == maxValue) ? 1 : 0;
15+
}
16+
if (dp[idx][current] != null) {
17+
return dp[idx][current];
18+
}
19+
int countWithout = countMaxOrSubsets(nums, idx + 1, current, maxValue, dp);
20+
int countWith = countMaxOrSubsets(nums, idx + 1, current | nums[idx], maxValue, dp);
21+
return dp[idx][current] = countWith + countWithout;
22+
}
23+
}

0 commit comments

Comments
 (0)