greedy simple solution


  • 0
    E

    public class Solution {
    public int wiggleMaxLength(int[] nums) {
    if(nums.length < 2) return nums.length;
    int diff=0,count =1;
    for(int i=0;i<nums.length-1;i++){
    if(diff==0){
    diff = nums[i]-nums[i+1];
    if(diff != 0) count++;
    }else if(diff*(nums[i]-nums[i+1]) < 0) {
    count++;
    diff = nums[i]-nums[i+1];
    }
    }
    return count;
    }
    }


Log in to reply
 

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