t03贪心算法_摆动序列
Sub 摆动序列() 'Dim nums(0 To 6) nums = Array(1, 17, 5, 10, 13, 15, 10, 5, 16, 8) ' nums = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) ' nums = Array(1, 7, 4, 9, 2, 5) Debug.Print (wiggleMaxLength(nums)) End Sub Function wiggleMaxLength(nums) If UBound(nums) <= 1 Then wiggleMaxLength = UBound(nums) End If Dim curDiff: curDiff = 0 Dim preDiff: preDiff = 0 Dim count: count = 1 For i = 1 To UBound(nums) curDiff = nums(i) - nums(i - 1) If ((curDiff > 0 And preDiff <= 0) Or (curDiff < 0 And preDiff >= 0)) Then count = count + 1 preDiff = curDiff End If Next wiggleMaxLength = count End Function