We have an array A of non-negative integers.

For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j].

Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)

Example 1:

Input: [0]
Output: 1
Explanation:
There is only one possible result: 0.

Example 2:

Input: [1,1,2]
Output: 3
Explanation:
The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.

Example 3:

Input: [1,2,4]
Output: 6
Explanation:
The possible results are 1, 2, 3, 4, 6, and 7.

Note:

  1. 1 <= A.length <= 50000
  2. 0 <= A[i] <= 10^9

Approach #1: Brute force. [C++] [TEL]

    int subarrayBitwiseORs1(vector<int>& A) {
int len = A.size();
set<int> ans;
for (int i = 0; i < len; ++i) {
for (int j = i; j < len; ++j) {
int temp = 0;
for (int k = i; k <= j; ++k) {
temp |= A[k];
}
ans.insert(temp);
}
} return ans.size();
}

  

Approach #2: DP[ ][ ]. [C++] [TEL]

    int subarrayBitwiseORs2(vector<int>& A) {
int len = A.size();
unordered_set<int> ans(begin(A), end(A));
vector<vector<int>> dp(len, vector<int>(len)); for (int l = 1; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
int j = i + l - 1;
if (l == 1) {
dp[i][j] = A[j];
continue;
} dp[i][j] = dp[i][j-1] | A[j];
ans.insert(dp[i][j]);
}
} return ans.size();
}

  

Approach #3: DP[ ]. [C++] [TEL]

    int subarrayBitwiseORs3(vector<int>& A) {
int len = A.size();
unordered_set<int> ans(begin(A), end(A));
vector<int> dp(A); for (int l = 2; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
ans.insert(dp[i] |= A[i+l-1]);
}
} return ans.size();
}

  

dp[i][j] = dp[i] | dp[i+1] | ..... | dp[j]

dp[i][j] = dp[i][j-1] | A[j]

ans = len(set(dp))

Time complexity: O(n^2)

Space complexity: O(n^2) -> O(n)

Approach #4: DP + Bit. [C++]

    int subarrayBitwiseORs(vector<int>& A) {
unordered_set<int> ans;
unordered_set<int> cur;
unordered_set<int> nxt; for (int a : A) {
nxt.clear();
nxt.insert(a);
for (int c : cur) {
nxt.insert(c | a);
}
cur.swap(nxt);
ans.insert(begin(cur), end(cur));
} return ans.size();
}

  

Approach #5: DP + Bit. [Java]

    public int subarrayBitwiseORs(int[] A) {
Set<Integer> ans = new HashSet<>();
Set<Integer> cur = new HashSet<>(); for (int a : A) {
Set<Integer> nxt = new HashSet<>();
nxt.add(a);
for (int b : cur) {
nxt.add(b | a);
}
ans.addAll(nxt);
cur = nxt;
} return ans.size();
}

  

Approach #6: DP + Bit. [Python]

class Solution(object):
def subarrayBitwiseORs(self, A):
"""
:type A: List[int]
:rtype: int
"""
cur = set()
ans = set() for a in A:
cur = {a | b for b in cur} | {a}
ans |= cur return len(ans)

  

Analysis:

Reference:

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-898-bitwise-ors-of-subarrays/

898. Bitwise ORs of Subarrays的更多相关文章

  1. [LeetCode] 898. Bitwise ORs of Subarrays 子数组按位或操作

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  2. 【LeetCode】898. Bitwise ORs of Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  3. LC 898. Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  4. [Swift]LeetCode898. 子数组按位或操作 | Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  5. 子序列的按位或 Bitwise ORs of Subarrays

    2018-09-23 19:05:20 问题描述: 问题求解: 显然的是暴力的遍历所有的区间是不可取的,因为这样的时间复杂度为n^2级别的,对于规模在50000左右的输入会TLE. 然而,最后的解答也 ...

  6. LeetCode编程训练 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  7. 算法与数据结构基础 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 对于Android开发,啥是高级工程师?

    最近一直在思考自己的技术方向.新的技术永远都是层出不穷,kotlin,flutter,小程序,轻应用等等,但是作为一个老鸟,新的东西,永远都是学不完的,想在新的技术上迭代学习出一个新高度,而增加自己的 ...

  2. JTSL/EL Expression学习

    最早的一个学习笔记,时间过去了久了,供java web初学者参考. JTSL/EL Expression学习安排 学习目标:掌握几个常见标签的使用,通晓工作原理,详细到代码层面,遇到问题时能查得出异常 ...

  3. MapReduceV1作业生命周期图解以及与YARN基本对比

    仿照<hadoop技术内幕:深入解析MapReduce架构设计与实现原理>中的原图,我用手绘制了一份类似的图-_- 4大部分:HDFS,Client,JobTracker,TaskTrac ...

  4. Laravel 认证原理及完全自定义认证

    Laravel 默认的 auth 功能已经是很全面了,但是我们也经常会碰到一些需要自定义的一些情况,比如验证的字段和默认的不匹配,比如需要能够同时满足 user name 和 email 认证等等.如 ...

  5. js如何实现网站title的滚动效果

    var text=document.title;//获得页面的标题            var timerID;//定时器            function newtext() {       ...

  6. java多线程同步(转)

    原文地址:http://developer.51cto.com/art/201509/490965.htm 一.场景 因为当我们有多个线程要同时访问一个变量或对象时,如果这些线程中既有读又有写操作时, ...

  7. 8.7 使用索引-notes

    七 正确使用索引 一 索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果,我们在添加索引时,必须遵循以下问题 1 范围问题,或者说条件不明确,条件中出现这 ...

  8. UVaLive 4452 The Ministers' Major Mess (TwoSat)

    题意:有 m 个人对 n 个方案投票,每个人最多只能对其中的4个方案投票(其他的相当于弃权),每一票要么支持要么反对.问是否存在一个最终决定,使得每个投票人都有超过一半的建议被采纳,在所有可能的最终决 ...

  9. Linux 系统中进程5中常见状态

    运行.中断.不可终端.僵死.停止 R(运行):正在运行 or 在运行队列中等待: S(中断):处于休眠中,等待接收信号,并脱离改状态: D(不可中断):不响应信号输入,即使kill也不起作用: Z(僵 ...

  10. 关闭父类弹出的ifream窗口

    parent.document.getElementById('zhuce').style.display = 'none';