We just need to find out all the maxima and minima, let's assume the number is `n`

, then the length of the longest subsequence is `n`

.

```
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
int len = nums.size();
if (len <= 1)
return len;
int res = (nums[0] != nums[1]) + 1;
int preDif = nums[1] - nums[0];
for (int i=2; i<len; i++) {
int dif = nums[i] - nums[i-1];
if (dif == 0)
continue;
if ((preDif < 0 && dif > 0) || (preDif > 0 && dif < 0))
res += 1;
if (res == 1)
res++;
preDif = dif;
}
return res;
}
};
```