0376.Wiggle Subsequence
class Solution {
public int wiggleMaxLength(int[] nums) {
int n = nums.length;
int count = 1;
int dir = 0; // 0 decrease 1 increase 2 no move
for(int i = 1; i < n; i++) {
if(i == 1) {
if(nums[i] > nums[0]) {
dir = 1;
count++;
}
else if(nums[i] < nums[0]) {
dir = 0;
count++;
}
else dir = 2;
continue;
}
if(nums[i] > nums[i-1]) {
if(dir != 1) {
count++;
dir = 1;
}
} else if(nums[i] < nums[i-1]) {
if(dir != 0) {
count++;
dir = 0;
}
}
}
return count;
}
}