Greedy Algorithms - Wiggle Subsequence
Consider the following Python function implementing the optimal wiggle subsequence algorithm. What is the value of
count after the loop finishes when the input is [1, 5, 4]?
def wiggleMaxLength(nums):
if not nums:
return 0
count = 1
last_diff = 0
for i in range(1, len(nums)):
diff = nums[i] - nums[i - 1]
if (diff > 0 and last_diff <= 0) or (diff < 0 and last_diff >= 0):
count += 1
last_diff = diff
return count
