Skip to content
Decoder

Wiggle Subsequence

Medium

Problem

A sequence wiggles if the differences between neighbours strictly alternate between positive and negative. A single number, or two different numbers, also count as wiggles. Equal neighbours (a difference of 0) break a wiggle. Return the length of the longest subsequence of nums that wiggles. A subsequence keeps the original order but may delete elements.

Example 1

Input
nums = [1, 7, 4, 9, 2, 5]
Output
6
Why
the whole list

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, 6, 7, 8, 9]
Output
2

Constraints

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.