Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 62a53f1

Browse files
committed
Add solution #3095
1 parent 4263c30 commit 62a53f1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,7 @@
21662166
3085|[Minimum Deletions to Make String K-Special](./solutions/3085-minimum-deletions-to-make-string-k-special.js)|Medium|
21672167
3090|[Maximum Length Substring With Two Occurrences](./solutions/3090-maximum-length-substring-with-two-occurrences.js)|Easy|
21682168
3091|[Apply Operations to Make Sum of Array Greater Than or Equal to k](./solutions/3091-apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.js)|Medium|
2169+
3095|[Shortest Subarray With OR at Least K I](./solutions/3095-shortest-subarray-with-or-at-least-k-i.js)|Easy|
21692170
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21702171
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21712172
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 3095. Shortest Subarray With OR at Least K I
3+
* https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i/
4+
* Difficulty: Easy
5+
*
6+
* You are given an array nums of non-negative integers and an integer k.
7+
*
8+
* An array is called special if the bitwise OR of all of its elements is at least k.
9+
*
10+
* Return the length of the shortest special non-empty subarray of nums, or return -1 if
11+
* no special subarray exists.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var minimumSubarrayLength = function(nums, k) {
20+
if (k === 0) return 1;
21+
22+
let minLength = Infinity;
23+
for (let start = 0; start < nums.length; start++) {
24+
let orResult = 0;
25+
for (let end = start; end < nums.length; end++) {
26+
orResult |= nums[end];
27+
if (orResult >= k) {
28+
minLength = Math.min(minLength, end - start + 1);
29+
break;
30+
}
31+
}
32+
}
33+
34+
return minLength === Infinity ? -1 : minLength;
35+
};

0 commit comments

Comments
 (0)