Skip to content

Commit ed90ea6

Browse files
committed
Sync LeetCode submission Runtime - 57 ms (100.00%), Memory - 50.9 MB (100.00%)
1 parent 20d6a80 commit ed90ea6

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>You are given an array <code>nums</code> of <strong>non-negative</strong> integers and an integer <code>k</code>.</p>
2+
3+
<p>An array is called <strong>special</strong> if the bitwise <code>OR</code> of all of its elements is <strong>at least</strong> <code>k</code>.</p>
4+
5+
<p>Return <em>the length of the <strong>shortest</strong> <strong>special</strong> <strong>non-empty</strong> <span data-keyword="subarray-nonempty">subarray</span> of</em> <code>nums</code>, <em>or return</em> <code>-1</code> <em>if no special subarray exists</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<div class="example-block">
11+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 2</span></p>
12+
13+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
14+
15+
<p><strong>Explanation:</strong></p>
16+
17+
<p>The subarray <code>[3]</code> has <code>OR</code> value of <code>3</code>. Hence, we return <code>1</code>.</p>
18+
</div>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<div class="example-block">
23+
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,8], k = 10</span></p>
24+
25+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
26+
27+
<p><strong>Explanation:</strong></p>
28+
29+
<p>The subarray <code>[2,1,8]</code> has <code>OR</code> value of <code>11</code>. Hence, we return <code>3</code>.</p>
30+
</div>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2], k = 0</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p>The subarray <code>[1]</code> has <code>OR</code> value of <code>1</code>. Hence, we return <code>1</code>.</p>
42+
</div>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
49+
<li><code>0 &lt;= nums[i] &lt;= 50</code></li>
50+
<li><code>0 &lt;= k &lt; 64</code></li>
51+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var minimumSubarrayLength = function(nums, k) {
7+
let result = Infinity;
8+
9+
for (let i = 0; i < nums.length; ++i) {
10+
let sum = 0;
11+
for (let j = i; j < nums.length; j++) {
12+
sum |= nums[j];
13+
if (sum >= k) {
14+
result = Math.min(result, (j - i + 1));
15+
}
16+
}
17+
}
18+
19+
return result === Infinity ? -1 : result;
20+
};

0 commit comments

Comments
 (0)