```
public static int wiggleMaxLength(int[] nums) {
int len = nums.length;
if (len < 2) {
return len;
}
// track pos and neg solution length
int pos = 1, neg = 1;
// record previous status
boolean posFlag = false, negFlag = false;
// record previous number
int lastPos = nums[0], lastNeg = nums[0];
for (int i = 1; i < len; i++) {
int cur = nums[i];
if (cur != lastPos) {
if ((cur > lastPos) ^ posFlag) {
pos++;
posFlag = !posFlag;
}
// update lastPos even this one is not in solution because the most recent one is easier to find next solution
lastPos = cur;
}
if (cur != lastNeg) {
if ((cur < lastNeg) ^ negFlag) {
neg++;
negFlag = !negFlag;
}
// same as Pos solution
lastNeg = cur;
}
}
return Math.max(pos,neg);
}
```