Skip to content

Commit 60a4870

Browse files
authored
Create 2401-longest-nice-subarray.js
1 parent 7529ce6 commit 60a4870

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

2401-longest-nice-subarray.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestNiceSubarray = function(nums) {
6+
let max = 1;
7+
let stack = [];
8+
for(let i =0;i<nums.length;i++){
9+
if(nums[i]&nums[i+1]) continue;
10+
stack.push(nums[i])
11+
for(let j =i+1;j<nums.length;j++){
12+
let state = true;
13+
for(el of stack){
14+
if(el&nums[j]){
15+
state=false;
16+
break;
17+
}
18+
}
19+
if(state) {
20+
stack.push(nums[j]);
21+
max = Math.max(max,stack.length);
22+
}
23+
else{
24+
stack = []
25+
break;
26+
}
27+
}
28+
}
29+
return max;
30+
};

0 commit comments

Comments
 (0)