Skip to content

Commit d84017c

Browse files
committed
add prob #898(2nd)
1 parent 5fabdf4 commit d84017c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: 898_Bitwise_ORs_of_Subarrays_2nd.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* O(N^2) in time and O(N^2) in space
2+
*
3+
* Could be N^2 in the worst scenario.
4+
* but this method is faster than the first one in practice
5+
*/
6+
7+
#include <vector>
8+
#include <unordered_set>
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
int subarrayBitwiseORs(vector<int>& A) {
14+
unordered_set<int> res;
15+
16+
for(int i=0; i<(int)A.size(); ++i){
17+
res.insert(A[i]);
18+
for(int j=i-1; j>=0; --j){
19+
if((A[j]|A[i])==A[j]) break;
20+
A[j] |= A[i];
21+
res.insert(A[j]);
22+
}
23+
}
24+
25+
return res.size();
26+
}
27+
};

0 commit comments

Comments
 (0)