Java O(n), 0 ms


  • 0
    V
    public class Solution 
    {
        public int wiggleMaxLength(int[] nums) 
        {
            if(nums == null || nums.length == 0)
                return 0;
            int cnt = 1;
            int [] tempArr = new int[nums.length - 1];
            for(int i = 1; i < nums.length; i++)
                tempArr[i - 1] = nums[i] - nums[i - 1] > 0? 1: nums[i] - nums[i - 1] < 0? 0: -1;
            int val, i = 0;
            while(i < tempArr.length && tempArr[i] == -1)
                i++;
            if(i == tempArr.length)
                return 1;
            val = tempArr[i];
            for(; i < tempArr.length; i++)
            {
                if(tempArr[i] != -1 && val != tempArr[i])
                {
                    cnt ++;
                    val = tempArr[i];   
                }
            }
            return cnt + 1;
        }
    }

Log in to reply
 

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