Skip to content

Wiggle Subsequence

MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.

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.

nums = [1, 7, 4, 9, 2, 5]                →  6   (the whole list)
nums = [1, 17, 5, 10, 13, 15, 10, 5, 16, 8]  →  7
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]       →  2

Constraints

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