C# solution


  • 0
    R

    """

    public int WiggleMaxLength(int[] nums) {
        if (nums == null || !nums.Any())
        {
            return 0;
        }
        
        if  (nums.Length == 1)
        {
            return 1;
        }
        
        int[] diff = new int[nums.Length-1];
        
        for(int i =1; i<nums.Length; i++)
        {
            diff[i-1] = nums[i]-nums[i-1];
        }
        
    
        int current = diff[0];
        int max = current == 0 ? 0 :1;
        
        for(int j = 1; j<diff.Length; j++)
        {
            if (diff[j] == 0)
            {
                continue;
            }
            
            if (DifferentSign(current, diff[j]))
            {
                max++;
                current = diff[j];
            }
        }
        
        return max+1;
    }
    
    private bool DifferentSign(int num1, int num2)
    {
        if (num1 == 0 && num2 != 0)
        {
            return true;
        }
        
        if(num1>0 && num2<0)
        {
            return true;
        }
        
        if (num1<0 && num2 >0)
        {
            return true;
        }
        
        return false;
    }
    

    """


Log in to reply
 

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