Leetcode 898. Bitwise ORs of Subarrays

转载自:https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165881/C%2B%2BJavaPython-O(30N)

1 描述

Description

  • 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 <= A.length <= 50000
  • 0 <= A[i] <= 10^9

2 代码

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

//时间复杂度O(N ^ 2)
//int subarrayBitwiseORs(vector<int>& A) {
//	unordered_set<int> us;
//	for (int i = 0; i < A.size(); i++)
//	{
//		int temp = 0;
//		for (int j = i; j < A.size(); j++)
//		{
//			temp |= A[j];
//			us.insert(temp);
//		}
//	}
//	return us.size();
//}

//时间复杂度O(30N)
int subarrayBitwiseORs(vector<int>& A) {
	unordered_set<int> res, cur, cur2;
	for (int i : A) {
		cur2 = { i };
		for (int j : cur) cur2.insert(i | j);
		cur = cur2;
		for (int j : cur) res.insert(j);
	}
	return res.size();
}

int main()
{
	vector<int> A = { 1,2,4 };
	int res = subarrayBitwiseORs(A);
	cout << res << endl;
	system("pause");
    return 0;
}

3 解释

Intuition
Assume B[i][j] = A[i] | A[i+1] | … | A[j]
Hash set cur stores all wise B[0][i], B[1][i], B[2][i], B[i][i].

When we handle the A[i+1], we want to update cur
So we need operate bitwise OR on all elements in cur.
Also we need to add A[i+1] to cur.

In each turn, we add all elements in cur to res.

Time Complexity:
O(30N)

Normally this part is easy.
But for this problem, time complexity matters a lot.

The solution is straight forward,
while you may worry about the time complexity up to O(N^2)
However, it’s not the fact.
This solution has only O(30N)

The reason is that, B[0][i] >= B[1][i] >= … >= B[i][i].
B[0][i] covers all bits of B[1][i]
B[1][i] covers all bits of B[2][i]

There are at most 30 bits for a positive number 0 <= A[i] <= 10^9.
So there are at most 30 different values for B[0][i], B[1][i], B[2][i], …, B[i][i].
Finally cur.size() <= 30 and res.size() <= 30 * A.length()

In a worst case, A = {1,2,4,8,16,…, 2 ^ 29}
And all B[i][j] are different and res.size() == 30 * A.length()

猜你喜欢

转载自blog.csdn.net/qq_24309981/article/details/90305070