KatInterview

376. Wiggle Subsequence

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the length of the longest subsequence from a given array nums that forms a "wiggle sequence". A wiggle sequence is defined by its differences between successive numbers strictly alternating between positive and negative.

Solution

Wiggle Subsequence in Python

class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        n = len(nums)
        # If the array has fewer than 2 elements, the length of the wiggle subsequence
        # is the number of elements itself.
        if n < 2:
            return n
        
        # 'up' stores the length of the longest wiggle subsequence ending with a positive difference.
        # 'down' stores the length of the longest wiggle subsequence ending with a negative difference.
        up = 1
        down = 1
        
        for i in range(1, n):
            if nums[i] > nums[i-1]:
                # Current element is greater, so it can form an "up" wiggle.
                # This extends the longest "down" wiggle sequence found so far.
                up = down + 1
            elif nums[i] < nums[i-1]:
                # Current element is smaller, so it can form a "down" wiggle.
                # This extends the longest "up" wiggle sequence found so far.
                down = up + 1
            # If nums[i] == nums[i-1], it doesn't extend the wiggle sequence.
            # The lengths 'up' and 'down' remain unchanged, as we can just replace
            # the previous element with the current one in a potential subsequence.
        
        # The result is the maximum of the two possibilities.
        return max(up, down)

Wiggle Subsequence in Java

class Solution {
    public int wiggleMaxLength(int[] nums) {
        // If the array has fewer than 2 elements, the length of the wiggle subsequence
        // is the number of elements itself.
        if (nums.length < 2) {
            return nums.length;
        }
        
        // 'up' stores the length of the longest wiggle subsequence ending with a positive difference.
        // 'down' stores the length of the longest wiggle subsequence ending with a negative difference.
        int up = 1;
        int down = 1;
        
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                // Current element is greater, so it can form an "up" wiggle.
                // This extends the longest "down" wiggle sequence found so far.
                up = down + 1;
            } else if (nums[i] < nums[i - 1]) {
                // Current element is smaller, so it can form a "down" wiggle.
                // This extends the longest "up" wiggle sequence found so far.
                down = up + 1;
            }
            // If nums[i] == nums[i-1], it doesn't extend the wiggle sequence.
            // The lengths 'up' and 'down' remain unchanged, as we can just replace
            // the previous element with the current one in a potential subsequence.
        }
        
        // The result is the maximum of the two possibilities.
        return Math.max(up, down);
    }
}

Wiggle Subsequence in C++

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        // If the array has fewer than 2 elements, the length of the wiggle subsequence
        // is the number of elements itself.
        if (nums.size() < 2) {
            return nums.size();
        }
        
        // 'up' stores the length of the longest wiggle subsequence ending with a positive difference.
        // 'down' stores the length of the longest wiggle subsequence ending with a negative difference.
        int up = 1;
        int down = 1;
        
        for (size_t i = 1; i < nums.size(); ++i) {
            if (nums[i] > nums[i-1]) {
                // Current element is greater, so it can form an "up" wiggle.
                // This extends the longest "down" wiggle sequence found so far.
                up = down + 1;
            } else if (nums[i] < nums[i-1]) {
                // Current element is smaller, so it can form a "down" wiggle.
                // This extends the longest "up" wiggle sequence found so far.
                down = up + 1;
            }
            // If nums[i] == nums[i-1], it doesn't extend the wiggle sequence.
            // The lengths 'up' and 'down' remain unchanged, as we can just replace
            // the previous element with the current one in a potential subsequence.
        }
        
        // The result is the maximum of the two possibilities.
        return max(up, down);
    }
};

Wiggle Subsequence in TypeScript

function wiggleMaxLength(nums: number[]): number {
    const n = nums.length;
    // If the array has fewer than 2 elements, the length of the wiggle subsequence
    // is the number of elements itself.
    if (n < 2) {
        return n;
    }

    // 'up' stores the length of the longest wiggle subsequence ending with a positive difference.
    // 'down' stores the length of the longest wiggle subsequence ending with a negative difference.
    let up = 1;
    let down = 1;

    for (let i = 1; i < n; i++) {
        if (nums[i] > nums[i - 1]) {
            // Current element is greater, so it can form an "up" wiggle.
            // This extends the longest "down" wiggle sequence found so far.
            up = down + 1;
        } else if (nums[i] < nums[i - 1]) {
            // Current element is smaller, so it can form a "down" wiggle.
            // This extends the longest "up" wiggle sequence found so far.
            down = up + 1;
        }
        // If nums[i] == nums[i-1], it doesn't extend the wiggle sequence.
        // The lengths 'up' and 'down' remain unchanged, as we can just replace
        // the previous element with the current one in a potential subsequence.
    }

    // The result is the maximum of the two possibilities.
    return Math.max(up, down);
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.