-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2411_Smallest_Subarrays_With_Maximum_Bitwise_OR.py
More file actions
67 lines (49 loc) · 2.57 KB
/
2411_Smallest_Subarrays_With_Maximum_Bitwise_OR.py
File metadata and controls
67 lines (49 loc) · 2.57 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
"""
You are given a 0-indexed array nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possible bitwise OR.
In other words, let Bij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(Bik) where i <= k <= n - 1.
The bitwise OR of an array is the bitwise OR of all the numbers in it.
Return an integer array answer of size n where answer[i] is the length of the minimum sized subarray starting at i with maximum bitwise OR.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,0,2,1,3]
Output: [3,3,2,2,1]
Explanation:
The maximum possible bitwise OR starting at any index is 3.
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
Therefore, we return [3,3,2,2,1].
Example 2:
Input: nums = [1,2]
Output: [2,1]
Explanation:
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
Therefore, we return [2,1].
Constraints:
n == nums.length
1 <= n <= 105
0 <= nums[i] <= 109
"""
class Solution:
def smallestSubarrays(self, nums: List[int]) -> List[int]:
n = len(nums)
answer = [0] * n
# Track the last position (index) where each bit (0 to 31) was seen
last_pos = [-1] * 32 # since max value is up to 2^30 (10^9)
# Traverse from right to left
for i in range(n - 1, -1, -1):
num = nums[i]
# Update last positions for bits set in nums[i]
for bit in range(32):
if (num >> bit) & 1:
last_pos[bit] = i
# The subarray must go up to the furthest index where any bit was last seen
max_len = 1 # at least length 1 (itself)
for pos in last_pos:
if pos != -1:
max_len = max(max_len, pos - i + 1)
answer[i] = max_len
return answer