Notice that array dp[] will not decrease.

curDiff records num[i]-nums[i-1]. preDiff mean the previous difference.

The naive thought is that if curDiff >0 && preDiff<0 or curDiff <0 && preDiff>0, length should increase 1.

But we need to be careful when num[i-1]-nums[i-2] == 0. If num[i-1]-nums[i-2] is 0, preDiff should record the previous difference that is not equal to 0. Or it will not contain any information. Then we can know next difference should be negative or positive.

```
public int wiggleMaxLength(int[] nums) {
if(nums==null || nums.length==0) return 0;
final int N = nums.length;
int[] dp = new int[N];
dp[0] = 1;
long curDiff=0, preDiff=0;
for(int i=1; i<N; i++){
if(curDiff != 0) preDiff = curDiff;
curDiff = (long)nums[i]-nums[i-1];
if((curDiff>0 && preDiff<=0) || (curDiff<0 && preDiff>=0)) {
dp[i] = dp[i-1]+1;
} else
dp[i] = dp[i-1];
}
return dp[N-1];
}
```