Greedy Algorithms - Wiggle Subsequence
The following code attempts to implement the optimal wiggle subsequence algorithm. Identify the line containing the subtle bug that causes incorrect results on inputs with consecutive equal elements.
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
