public int wiggleMaxLength(int[] nums){
  if (nums == null || nums.length == 0) {
    return 0;
  }
  int inc=1;
  int dec=1;
  for (int i=1; i < nums.length; i++) {
    if (nums[i] > nums[i - 1]) {
      inc=dec + 1;
    }
 else     if (nums[i] < nums[i - 1]) {
      dec=inc + 1;
    }
  }
  return Math.max(inc,dec);
}

public int wiggleMaxLength ( int [ ] nums ) { if ( nums == null || nums . length == 0 ) { return 0 ; } int inc = 1 ; int dec = 1 ; for ( int i = 1 ; i < nums . length ; i ++ ) { if ( nums [ i ] > nums [ i - 1 ] ) { inc = dec + 1 ; } else if ( nums [ i ] < nums [ i - 1 ] ) { dec = inc + 1 ; } } return Math . max ( inc , dec ) ; }
dp , greedy . use two vars to record the count of increasing - first and decreasing - first sequences . then loop through the array from second number . if current number is larger than previous one , it means we can extend our last decreasing sequence length by 1 . so inc = dec + 1 . otherwise , dec = inc + 1 . return the max of these two counts .
