public int wiggleMaxLength(int[] nums) {
if (nums == null) return 0;
if (nums.length <= 1) return nums.length;
int f = 1, b = 1; //the first number can be a smaller number or larger number depends on what the next number it is.
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i1]) f = b + 1;
else if (nums[i] < nums[i1]) b = f + 1;
}
return Math.max(f, b);
}
Java Greedy solution O(n) time complexity, O(1) space complexity



@haili2 What happen if the array is { 1, 2, 1, 2,3,1,4,1,5,1,6} or { 1, 2, 1, 1,1,1,4,1,5,1,6 } ?