We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5fabdf4 commit d84017cCopy full SHA for d84017c
898_Bitwise_ORs_of_Subarrays_2nd.cpp
@@ -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