class Solution(object):
def wiggleMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) is 1:
return 1
cnt, maxi = 1, 0
flag = None
for i in range(1, len(nums)):
diff = nums[i] - nums[i - 1]
if diff != 0 and flag is None:
flag = diff > 0
cnt = 2
elif flag and diff < 0 or not flag and diff > 0:
cnt += 1
flag = not flag
maxi = max(maxi, cnt)
return maxi
public class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums == null || nums.length < 1) return 0;
int high = 1;
int low = 1;
int len = nums.length;
for (int i = 1; i < len; i++) {
if (nums[i] > nums[i - 1]) high = low + 1;
else if (nums[i] < nums[i - 1]) low = high + 1;
}
return Math.max(high, low);
}
}