bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 376. Wiggle Subsequence

376. Wiggle Subsequence

Leetcode Solutions

Approach # Greedy Approach

  1. Initialize count to 1 (since the first element is always part of the wiggle subsequence).
  2. Iterate through the array starting from the second element.
  3. Calculate the difference diff between the current element and the previous one.
  4. If diff is positive and prevdiff is non-positive, or diff is negative and prevdiff is non-negative, increment count.
  5. If diff is not zero, update prevdiff to diff.
  6. Continue to the next element and repeat steps 3-5.
  7. Return count as the length of the longest wiggle subsequence.
UML Thumbnail

Approach # Dynamic Programming

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...