Wiggle Subsequence
Medium
Topics
A wiggle sequence has strictly alternating positive and negative differences between successive numbers. Given nums, return the length of the longest wiggle subsequence.
Example 1
Input: nums = [1,7,4,9,2,5] Output: 6
Example 2
Input: nums = [1,17,5,10,13,15,10,5,16,8] Output: 7
Example 3
Input: nums = [1,2,3,4,5] Output: 2
Constraints
- 1 <= nums.length <= 1000
- 0 <= nums[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
A wiggle subsequence alternates rise and fall, so at every position only two facts matter: the length of the best subsequence ending here that arrived going up, and the best that arrived going down. Two integers carry the entire state.
The update is almost too simple to trust. When the array rises from the previous element, any down-ending subsequence can be extended by this element, so up = down + 1; when it falls, down = up + 1. Equal neighbours change nothing. The reason this greedy is exact is that only the direction of the last step constrains what can follow — the actual values never matter again, so there is nothing else to remember.
Approach
- Return the length directly for arrays shorter than two.
- Start
up = down = 1. - Walk from index 1: on a rise set
up = down + 1, on a fall setdown = up + 1, on a tie do nothing. - Return
max(up, down).
Dry Run
nums = [1,7,4,9,2,5]
i nums[i] vs prev up down
0 1 - 1 1
1 7 rose 2 1
2 4 fell 2 3
3 9 rose 4 3
4 2 fell 4 5
5 5 rose 6 5
answer max(up, down) = 6
[1,17,5,10,13,15,10,5,16,8] -> 7
[1,2,3,4,5] -> 2 one rise is all you get
[1,1,1] -> 1 plateaus update neither counter
[3] -> 1Complexity
- Time — O(n): one pass, two comparisons per element.
- Space — O(1): two integers. The O(n²) dynamic program over all pairs is the natural first answer and is worth stating before collapsing it to this.
Common Pitfalls
- Updating both counters on the same step. A rise tells you nothing new about the best down-ending subsequence, so
downmust be left alone — and writingup, down = down + 1, up + 1on a rise corrupts it. - Reading the previous value from
upanddownafter one has already been updated. In a language without simultaneous assignment the order matters; here only one of the two changes per step, so it does not — but the reason is worth knowing. - Counting direction changes in the raw array. That is the same answer only if you first collapse plateaus; doing it directly makes
[1,1,2]come out wrong. - Treating equal adjacent elements as a rise or a fall. A flat step cannot be part of a wiggle, so it updates nothing.
- Returning
upordownalone. The best subsequence may end in either direction. - Trying to reconstruct the actual subsequence from these two counters. They are lengths only; recovering the elements needs the O(n²) table or a separate greedy pass.
Full Solution — all languages
class Solution:
def wiggleMaxLength(self, nums):
if len(nums) < 2:
return len(nums)
up = down = 1
for i in range(1, len(nums)):
if nums[i] > nums[i-1]:
up = down + 1
elif nums[i] < nums[i-1]:
down = up + 1
return max(up, down)import java.util.*;
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length < 2) return nums.length;
int up = 1, down = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) up = down + 1;
else if (nums[i] < nums[i-1]) down = up + 1;
}
return Math.max(up, down);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int wiggleMaxLength(vector<int>& nums) {
if (nums.size() < 2) return nums.size();
int up = 1, down = 1;
for (size_t i = 1; i < nums.size(); i++) {
if (nums[i] > nums[i-1]) up = down + 1;
else if (nums[i] < nums[i-1]) down = up + 1;
}
return max(up, down);
}
};/**
* @param {number[]} nums
* @return {number}
*/ var wiggleMaxLength = function(nums) {
if (nums.length < 2) return nums.length;
let up = 1, down = 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) up = down + 1;
else if (nums[i] < nums[i-1]) down = up + 1;
}
return Math.max(up, down);
};