leet-code/wiggle-subsequence/sol.go

18 lines
438 B
Go
Raw Normal View History

2022-07-05 23:33:16 +00:00
package main
func wiggleMaxLength(nums []int) int {
// Current's value only matters in terms of negative / 0 / positive.
// The numerical value doesn't get used and therefore can be messed with.
current, count := 0, len(nums)
for i := 0; i < len(nums)-1; i++ {
new := nums[i+1] - nums[i]
if (current > 0 && new > 0) || (current < 0 && new < 0) || new == 0 {
count--
}
if new != 0 {
current = new
}
}
return count
}