Wiggle Subsequence - Problem

Imagine you're tracking a roller coaster's ups and downs - that's exactly what a wiggle sequence is! A wiggle sequence alternates between going up and down, where the differences between consecutive numbers strictly alternate between positive and negative.

๐ŸŽข What makes a sequence "wiggle"?

  • The differences between successive numbers must strictly alternate between positive and negative
  • The first difference can be either positive or negative
  • Single elements and two non-equal elements are trivially wiggle sequences

Examples:

[1, 7, 4, 9, 2, 5] โœ… Wiggle! Differences: (6, -3, 5, -7, 3) - alternates perfectly

[1, 4, 7, 2, 5] โŒ Not wiggle! First two differences (3, 3) are both positive

[1, 7, 4, 5, 5] โŒ Not wiggle! Last difference is zero

Your Mission: Given an integer array nums, find the length of the longest wiggle subsequence. Remember, a subsequence maintains the original order but can skip elements.

Input & Output

example_1.py โ€” Basic Wiggle Sequence
$ Input: [1,7,4,9,2,5]
โ€บ Output: 6
๐Ÿ’ก Note: The entire array forms a wiggle sequence with differences [6,-3,5,-7,3] that alternate perfectly between positive and negative. All 6 elements can be included.
example_2.py โ€” Non-Wiggle Sequence
$ Input: [1,4,7,2,5]
โ€บ Output: 4
๐Ÿ’ก Note: The differences are [3,3,-5,3]. The first two differences are both positive, so we need to skip one element. Optimal subsequence: [1,7,2,5] with differences [6,-5,3].
example_3.py โ€” Edge Case
$ Input: [1,2,3,4,5]
โ€บ Output: 2
๐Ÿ’ก Note: This is a strictly increasing sequence. The longest wiggle subsequence we can form has length 2, like [1,5] or [1,2]. Any two non-equal elements form a valid wiggle sequence.

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 0 โ‰ค nums[i] โ‰ค 1000
  • Follow up: Could you solve this in O(n) time?

Visualization

Tap to expand
๐ŸŽข Building the Longest Wiggle TrackHeights: [1, 7, 4, 9, 2, 5]174925โฌ† +6โฌ‡ -3โฌ† +5โฌ‡ -7โฌ† +3Track Ending โฌ† (UP)Step 1: up=1, down=1Step 2: up=2 (extended down+1)Step 4: up=4 (extended down+1)Step 6: up=6 (extended down+1)Track Ending โฌ‡ (DOWN)Step 1: up=1, down=1Step 3: down=3 (extended up+1)Step 5: down=5 (extended up+1)Final: down=5๐Ÿ† Result: max(up=6, down=5) = 6The optimal wiggle sequence includes all 6 elements!
Understanding the Visualization
1
Survey the Terrain
Look at the height differences between consecutive points [1,7,4,9,2,5] โ†’ [+6,-3,+5,-7,+3]
2
Track Two Possibilities
At each point, maintain the longest track ending upward and ending downward
3
Make Optimal Choices
When going up, extend the best downward track. When going down, extend the best upward track
4
Build the Track
The final answer is the longer of the two possible tracks we've been building
Key Takeaway
๐ŸŽฏ Key Insight: We don't need to store actual sequences - just track the lengths of the best sequences ending in each direction. This reduces space complexity from O(n) to O(1) while maintaining optimal O(n) time!
Asked in
Microsoft 15 Amazon 12 Google 8 Meta 6
67.9K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen