Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b390b6

Browse files
committed
Sync LeetCode submission Runtime - 960 ms (41.04%), Memory - 29.1 MB (52.24%)
1 parent 246d7cd commit 0b390b6

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code>, consisting of non-negative integers. For each index <code>i</code> from <code>0</code> to <code>n - 1</code>, you must determine the size of the <strong>minimum sized</strong> non-empty subarray of <code>nums</code> starting at <code>i</code> (<strong>inclusive</strong>) that has the <strong>maximum</strong> possible <strong>bitwise OR</strong>.</p>
2+
3+
<ul>
4+
<li>In other words, let <code>B<sub>ij</sub></code> be the bitwise OR of the subarray <code>nums[i...j]</code>. You need to find the smallest subarray starting at <code>i</code>, such that bitwise OR of this subarray is equal to <code>max(B<sub>ik</sub>)</code> where <code>i &lt;= k &lt;= n - 1</code>.</li>
5+
</ul>
6+
7+
<p>The bitwise OR of an array is the bitwise OR of all the numbers in it.</p>
8+
9+
<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>n</code><em> where </em><code>answer[i]</code><em> is the length of the <strong>minimum</strong> sized subarray starting at </em><code>i</code><em> with <strong>maximum</strong> bitwise OR.</em></p>
10+
11+
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [1,0,2,1,3]
18+
<strong>Output:</strong> [3,3,2,2,1]
19+
<strong>Explanation:</strong>
20+
The maximum possible bitwise OR starting at any index is 3.
21+
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
22+
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
23+
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
24+
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
25+
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
26+
Therefore, we return [3,3,2,2,1].
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [1,2]
33+
<strong>Output:</strong> [2,1]
34+
<strong>Explanation:
35+
</strong>Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
36+
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
37+
Therefore, we return [2,1].
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>n == nums.length</code></li>
45+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
46+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
47+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Approach: Enumeration
2+
3+
class Solution:
4+
def smallestSubarrays(self, nums: List[int]) -> List[int]:
5+
n = len(nums)
6+
pos = [-1] * 31
7+
ans = [0] * n
8+
9+
for i in range(n - 1, -1, -1):
10+
j = i
11+
for bit in range(31):
12+
if (nums[i] & (1 << bit)) == 0:
13+
if pos[bit] != -1:
14+
j = max(j, pos[bit])
15+
else:
16+
pos[bit] = i
17+
ans[i] = j - i + 1
18+
return ans
19+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /