-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.js
More file actions
39 lines (33 loc) · 989 Bytes
/
solution.js
File metadata and controls
39 lines (33 loc) · 989 Bytes
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
// Solution for LeetCode Problem #2411: Smallest Subarrays With Maximum Bitwise OR
// Date: 2025-07-29
// Difficulty: Medium
// Language: JavaScript
/**
* @param {number[]} nums
* @return {number[]}
*/
var smallestSubarrays = function(nums) {
const n = nums.length;
const result = new Array(n);
// For each starting position
for (let i = 0; i < n; i++) {
let currentOR = 0;
let minLength = 1;
// Calculate the maximum possible OR value starting from position i
let maxOR = 0;
for (let j = i; j < n; j++) {
maxOR |= nums[j];
}
// Find the smallest subarray that achieves the maximum OR
currentOR = 0;
for (let j = i; j < n; j++) {
currentOR |= nums[j];
if (currentOR === maxOR) {
minLength = j - i + 1;
break;
}
}
result[i] = minLength;
}
return result;
};