# Concise Java Solution (DP, 0ms)

• ``````public class Solution {
public int wiggleMaxLength(int[] nums) {
int n = nums.length, count = 2;
if(n<=2) return n;
boolean sign = nums[1]-nums[0]<0 ? true : false;
for(int i=2; i<n; i++){
if(nums[i]==nums[i-1] || (nums[i]-nums[i-1]<0) == sign) continue;
if(nums[i]!=nums[i-1]) { count++; sign=!sign; }
}
return count;
}
}
``````

• Fails for example `[0,0]`.

Also, `? true : false` rather looks like the opposite of concise.

• @StefanPochmann You‘d better read the conditions with patience.
The first difference (if one exists) may be either positive or negative.
Understand?

• @EncoreSummer That's for wiggle sequences, not for the input.

• @StefanPochmann
Taking the repeated nums at the beginning into consideration.

``````public class Solution {
public int wiggleMaxLength(int[] nums) {
int n = nums.length, j=0;
while(j<n-1 && nums[j] == nums[j+1]) j++;
if(n <= j+2) return n-j;
int count = 2;
boolean sign = (nums[j+1]-nums[j]<0);
for(int i=j+2; i<n; i++){
if(nums[i]==nums[i-1] || (nums[i]-nums[i-1]<0) == sign) continue;
if(nums[i]!=nums[i-1]) { count++; sign=!sign; }
}
return count;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.