Skip to content

Commit 7ca6ae2

Browse files
committed
Add solution #898
1 parent dff60ca commit 7ca6ae2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@
708708
895|[Maximum Frequency Stack](./0895-maximum-frequency-stack.js)|Hard|
709709
896|[Monotonic Array](./0896-monotonic-array.js)|Easy|
710710
897|[Increasing Order Search Tree](./0897-increasing-order-search-tree.js)|Easy|
711+
898|[Bitwise ORs of Subarrays](./0898-bitwise-ors-of-subarrays.js)|Medium|
711712
901|[Online Stock Span](./0901-online-stock-span.js)|Medium|
712713
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
713714
909|[Snakes and Ladders](./0909-snakes-and-ladders.js)|Medium|
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 898. Bitwise ORs of Subarrays
3+
* https://leetcode.com/problems/bitwise-ors-of-subarrays/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty
7+
* subarrays of arr.
8+
*
9+
* The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise
10+
* OR of a subarray of one integer is that integer.
11+
*
12+
* A subarray is a contiguous non-empty sequence of elements within an array.
13+
*/
14+
15+
/**
16+
* @param {number[]} arr
17+
* @return {number}
18+
*/
19+
var subarrayBitwiseORs = function(arr) {
20+
const results = new Set();
21+
let previous = new Set();
22+
23+
for (const num of arr) {
24+
const current = new Set([num]);
25+
for (const prev of previous) {
26+
current.add(prev | num);
27+
}
28+
previous = current;
29+
previous.forEach(val => results.add(val));
30+
}
31+
32+
return results.size;
33+
};

0 commit comments

Comments
 (0)