LeetCode Question - 376. Wiggle Subsequence ๐Ÿง 

LeetCode Question - 376. Wiggle Subsequence ๐Ÿง 

3rd July 2022 | ๐Ÿ—“ Daily LeetCode Challenge - #3

ยท

3 min read

About the Series

Problem-solving is a key skill set for any tech-related stuff you might be working on.

When it comes to developers it's one of the most crucial skills which is needed in almost any day-to-day code you might be writing.

So, this series of blogs is all about practicing Daily LeetCode Challenges & Problem-solving. ๐Ÿš€

Problem Statement

Wiggle Subsequence

A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.

  • For example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative.
  • In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero. A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.

Given an integer array nums, return the length of the longest wiggle subsequence of nums.

Video Explanation

Sketch-annotation-element-stroke-line-arrow-spiral-up.png

Solution

Algorithm

  1. First check if the length of the nums array is smaller than two or not. If yes, we will simply return the length of the array ie. 0 for [] and 1 for [x] (where x can be any value)
  2. We check the first difference between the 1st from the 0th value in the nums array and store it in the previousdiff
  3. If the values are not equal we will count both the no. for the wiggle sequence thus our counter will be 2 and if equal we take our counter to be 1
  4. Now, While iterating the nums array we will compare the compare current difference and previous difference. If one is positive the other should be negative or vice versa. Now, we will increment the count if the conditions are satisfied and update the previous difference with the current difference.
  5. Finally return the count of the wiggle sequence.

Code in JS ๐Ÿง‘โ€๐Ÿ’ป

/**
 * @param {number[]} nums
 * @return {number}
 */
var wiggleMaxLength = function (nums) {
  if (nums.length < 2) {
    return nums.length;
  }
  var previousdiff = nums[1] - nums[0];
  var counter = previousdiff != 0 ? 2 : 1;

  for (var i = 1; i < nums.length; i++) {
    const currentDiff = nums[i] - nums[i - 1];
    if (
      (currentDiff > 0 && previousdiff <= 0) ||
      (currentDiff < 0 && previousdiff >= 0)
    ) {
      counter++;
      previousdiff = currentDiff;
    }
  }
  return counter;
};

Time Complexity : O(n)

Space Complexity: O(1)

Similar Questions for practice

Now it is time to try more similar questions

You can find me on the web ๐ŸŒ

Add your solution or approach in the comments below. Also, show your love by Sharing the blog. ๐Ÿค—

"Dream big. Start small. But most of all, start."

~ Simon Sinek

Did you find this article valuable?

Support Sourav Dey by becoming a sponsor. Any amount is appreciated!

ย