Skip to content

Commit 49bb420

Browse files
authored
Create 3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js
1 parent a5b7b3e commit 49bb420

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestMonotonicSubarray = function(nums) {
6+
const n = nums.length
7+
return Math.max(inc(), desc())
8+
9+
function inc() {
10+
let res = 1, cur = 1
11+
for(let i = 1; i < n; i++) {
12+
if(nums[i] > nums[i - 1]) {
13+
cur++
14+
res = Math.max(res, cur)
15+
} else {
16+
cur = 1
17+
}
18+
}
19+
20+
return res
21+
}
22+
23+
function desc() {
24+
let res = 1, cur = 1
25+
for(let i = 1; i < n; i++) {
26+
if(nums[i] < nums[i - 1]) {
27+
cur++
28+
res = Math.max(res, cur)
29+
} else {
30+
cur = 1
31+
}
32+
}
33+
34+
return res
35+
}
36+
};

0 commit comments

Comments
 (0)