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

Commit 121c48b

Browse files
committed
Add solution #376
1 parent 10bfb73 commit 121c48b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

0376-wiggle-subsequence.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 376. Wiggle Subsequence
3+
* https://leetcode.com/problems/wiggle-subsequence/
4+
* Difficulty: Medium
5+
*
6+
* A wiggle sequence is a sequence where the differences between successive numbers strictly
7+
* alternate between positive and negative. The first difference (if one exists) may be either
8+
* positive or negative. A sequence with one element and a sequence with two non-equal elements
9+
* are trivially wiggle sequences.
10+
* - For example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3)
11+
* alternate between positive and negative.
12+
* - In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not
13+
* because its first two differences are positive, and the second is not because its last
14+
* difference is zero.
15+
*
16+
* A subsequence is obtained by deleting some elements (possibly zero) from the original sequence,
17+
* leaving the remaining elements in their original order.
18+
*
19+
* Given an integer array nums, return the length of the longest wiggle subsequence of nums.
20+
*/
21+
22+
/**
23+
* @param {number[]} nums
24+
* @return {number}
25+
*/
26+
var wiggleMaxLength = function(nums) {
27+
let up = 1;
28+
let down = 1;
29+
30+
for (let i = 1; i < nums.length; i++) {
31+
if (nums[i] > nums[i - 1]) {
32+
up = down + 1;
33+
} else if (nums[i] < nums[i - 1]) {
34+
down = up + 1;
35+
}
36+
}
37+
38+
return Math.max(up, down);
39+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
373|[Find K Pairs with Smallest Sums](./0373-find-k-pairs-with-smallest-sums.js)|Medium|
296296
374|[Guess Number Higher or Lower](./0374-guess-number-higher-or-lower.js)|Medium|
297297
375|[Guess Number Higher or Lower II](./0375-guess-number-higher-or-lower-ii.js)|Medium|
298+
376|[Wiggle Subsequence](./0376-wiggle-subsequence.js)|Medium|
298299
378|[Kth Smallest Element in a Sorted Matrix](./0378-kth-smallest-element-in-a-sorted-matrix.js)|Medium|
299300
380|[Insert Delete GetRandom O(1)](./0380-insert-delete-getrandom-o1.js)|Medium|
300301
383|[Ransom Note](./0383-ransom-note.js)|Easy|

0 commit comments

Comments
 (0)