A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
For example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative. In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.
A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
Given an integer array nums
, return the length of the longest wiggle subsequence of nums
.
Example 1:
Input: nums = [1,7,4,9,2,5] Output: 6 Explanation: The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
Example 2:
Input: nums = [1,17,5,10,13,15,10,5,16,8] Output: 7 Explanation: There are several subsequences that achieve this length. One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
Example 3:
Input: nums = [1,2,3,4,5,6,7,8,9] Output: 2
Constraints:
Could you solve this in O(n) time?
Given an integer array nums
, find the length of the longest wiggle subsequence.
A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
For example, [1, 7, 4, 9, 2, 5]
is a wiggle sequence because the differences (6, -3, 5, -7, 3)
alternate between positive and negative. In contrast, [1, 4, 7, 2, 5]
and [1, 7, 4, 5, 5]
are not wiggle sequences.
A brute force approach would involve generating all possible subsequences of the given array and checking each subsequence to see if it's a wiggle sequence. Then, keep track of the length of the longest wiggle subsequence found. This approach is highly inefficient.
We can solve this problem using dynamic programming or a greedy approach in O(n) time.
Let up[i]
be the length of the longest wiggle subsequence ending at index i
with a positive difference. Let down[i]
be the length of the longest wiggle subsequence ending at index i
with a negative difference.
Then, the recurrence relations are:
up[i] = up[j] + 1
if nums[i] > nums[j]
and j < i
down[i] = down[j] + 1
if nums[i] < nums[j]
and j < i
A more efficient approach can be achieved with a greedy algorithm. The main idea is to keep track of the last peak and valley seen so far. We iterate through the array and update the peak and valley accordingly.
up = 1
and down = 1
.nums[i] > nums[i-1]
, it means we have a potential up-wiggle. Update up = down + 1
.nums[i] < nums[i-1]
, it means we have a potential down-wiggle. Update down = up + 1
.max(up, down)
.def wiggleMaxLength(nums):
if not nums:
return 0
up = 1
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)
up
and down
variables.nums = [1, 7, 4, 9, 2, 5]
up = 1
, down = 1
i = 1
, nums[1] = 7 > nums[0] = 1
, up = down + 1 = 2
i = 2
, nums[2] = 4 < nums[1] = 7
, down = up + 1 = 3
i = 3
, nums[3] = 9 > nums[2] = 4
, up = down + 1 = 4
i = 4
, nums[4] = 2 < nums[3] = 9
, down = up + 1 = 5
i = 5
, nums[5] = 5 > nums[4] = 2
, up = down + 1 = 6
Return max(up, down) = 6