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.)
Input: [0]
Output: 1
Explanation:
There is only one possible result: 0.
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.
Input: [1,2,4]
Output: 6
Explanation:
The possible results are 1, 2, 3, 4, 6, and 7.
Note:
1 <= A.length <= 50000
0 <= A[i] <= 10^9
这道题很好理解,但是时间复杂度要求很高,就连普通DP都会超时,容我慢慢道来。
1、直接暴力(TLE):
这样,时间复杂度 O(n^3),空间复杂度 O(1),肯定不行,想都不用想先pass。
2、普通DP(我使用的方法,但是很可惜还是 TLE 了):
dp[i][j] = A[i] | A[i+1] | ... | A[j]
,其中 i >= j;dp[i][j] = dp[i][j-1] + A[j]
这样,时间复杂度 O(n^2),空间复杂度 O(n^2),还是超时了......
3、高级DP(AC):
dp[i] = [(b | A[i]) for b in dp[i-1]] + A[i]
,即以 A[i] 结尾的所有子数组 Or 结果等于以 A[i-1] 结尾的所有子数组 Or 结果,和当前的 A[i] 进行 Or 操作,再加上 A[i] 这个结果。注意: len(dp) <= 32,后面会给出证明。这时,时间复杂度 O(32*n),空间复杂度 O(32*n),接受。
证明:为什么方法 3 中的 len(dp) <= 32 ?
dp[i] = { A[i], A[i]|A[i-1], A[i]|A[i-1]|A[i-2], … , A[i]|A[i-1]|…|A[0] }
,这个序列单调递增,通过把 A[i] 中的 0 变成 1。A[i] 最多有 32 个 0,所以这个集合的大小 <= 32。 举例:最坏情况 A = [8, 4, 2, 1, 0],都是 2^k。 A[5] = 0,dp[5] = { 0, 0|1, 0|1|2, 0|1|2|4, 0|1|2|4| } = { 0, 1, 3, 7, 15 }.
class Solution:
def subarrayBitwiseORs(self, A: List[int]) -> int:
lens = len(A)
dp = [[0] * lens for _ in range(lens)]
res = set()
for i in range(lens):
dp[i][i] = A[i]
res.add(dp[i][i])
for j in range(i+1, lens):
dp[i][j] = dp[i][j-1] | A[j]
res.add(dp[i][j])
return len(res)
class Solution(object):
def subarrayBitwiseORs(self, A):
"""
:type A: List[int]
:rtype: int
"""
res = set()
cur = set() # 保存上一次的结果
for a in A:
cur = {n | a for n in cur} | {a}
res |= cur # 更新结果
return len(res)