Skip to content

Commit b927d90

Browse files
committed
Add solution #2044
1 parent 1e35b13 commit b927d90

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,706 LeetCode solutions in JavaScript
1+
# 1,707 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1567,6 +1567,7 @@
15671567
2040|[Kth Smallest Product of Two Sorted Arrays](./solutions/2040-kth-smallest-product-of-two-sorted-arrays.js)|Hard|
15681568
2042|[Check if Numbers Are Ascending in a Sentence](./solutions/2042-check-if-numbers-are-ascending-in-a-sentence.js)|Easy|
15691569
2043|[Simple Bank System](./solutions/2043-simple-bank-system.js)|Medium|
1570+
2044|[Count Number of Maximum Bitwise-OR Subsets](./solutions/2044-count-number-of-maximum-bitwise-or-subsets.js)|Medium|
15701571
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
15711572
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
15721573
2071|[Maximum Number of Tasks You Can Assign](./solutions/2071-maximum-number-of-tasks-you-can-assign.js)|Hard|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2044. Count Number of Maximum Bitwise-OR Subsets
3+
* https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return
7+
* the number of different non-empty subsets with the maximum bitwise OR.
8+
*
9+
* An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero)
10+
* elements of b. Two subsets are considered different if the indices of the elements chosen are
11+
* different.
12+
*
13+
* The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var countMaxOrSubsets = function(nums) {
21+
const maxOr = nums.reduce((or, num) => or | num, 0);
22+
let count = 0;
23+
24+
backtrack(0, 0);
25+
26+
return count;
27+
28+
function backtrack(index, currentOr) {
29+
if (currentOr === maxOr) count++;
30+
for (let i = index; i < nums.length; i++) {
31+
backtrack(i + 1, currentOr | nums[i]);
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)