-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (60 loc) · 1.71 KB
/
main.go
File metadata and controls
67 lines (60 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Source: https://leetcode.com/problems/bitwise-ors-of-subarrays
// Title: Bitwise ORs of Subarrays
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an integer array `arr`, return the number of distinct bitwise ORs of all the non-empty subarrays of `arr`.
//
// The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
//
// A **subarray** is a contiguous non-empty sequence of elements within an array.
//
// **Example 1:**
//
// ```
// Input: arr = [0]
// Output: 1
// Explanation: There is only one possible result: 0.
// ```
//
// **Example 2:**
//
// ```
// Input: arr = [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: arr = [1,2,4]
// Output: 6
// Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
// ```
//
// **Constraints:**
//
// - `1 <= arr.length <= 5 * 10^4`
// - `0 <= arr[i] <= 10^9`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func subarrayBitwiseORs(arr []int) int {
type void struct{}
ans := make(map[int]void)
var currMap map[int]void
for _, num := range arr {
nextMap := make(map[int]void)
nextMap[num] = void{}
ans[num] = void{}
for curr := range currMap {
nextMap[curr|num] = void{}
ans[curr|num] = void{}
}
currMap = nextMap
}
return len(ans)
}