Skip to content

Commit b496402

Browse files
committed
Add solution #3105
1 parent 98a1bd9 commit b496402

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
2727|[Is Object Empty](./2727-is-object-empty.js)|Easy|
545545
2948|[Make Lexicographically Smallest Array by Swapping Elements](./2948-make-lexicographically-smallest-array-by-swapping-elements.js)|Medium|
546546
3042|[Count Prefix and Suffix Pairs I](./3042-count-prefix-and-suffix-pairs-i.js)|Easy|
547+
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
547548
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
548549
3151|[Special Array I](./3151-special-array-i.js)|Easy|
549550
3223|[Minimum Length of String After Operations](./3223-minimum-length-of-string-after-operations.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 3105. Longest Strictly Increasing or Strictly Decreasing Subarray
3+
* https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/
4+
* Difficulty: Easy
5+
*
6+
* You are given an array of integers nums. Return the length of the longest subarray of nums
7+
* which is either strictly increasing or strictly decreasing.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var longestMonotonicSubarray = function(nums) {
15+
let result = nums.length > 0 ? 1 : 0;
16+
for (let i = 1, down = 1, up = 1; i < nums.length; i++) {
17+
nums[i - 1] > nums[i] ? down++ : down = 1;
18+
nums[i - 1] < nums[i] ? up++ : up = 1;
19+
result = Math.max(result, down, up);
20+
}
21+
return result;
22+
};

0 commit comments

Comments
 (0)