Skip to main content

Command Palette

Search for a command to run...

LeetCode Question - 376. Wiggle Subsequence ๐Ÿง 

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

Published
โ€ข3 min read
LeetCode Question - 376. Wiggle Subsequence ๐Ÿง 
S

Hi folks! I am a ๐Ÿ‘จโ€๐Ÿ’ป Full-Stack Developer, occasional Designer, and Blogger facilitating the world with User Experience ๐Ÿง as a Design Thinker ๐Ÿ’ญ and User-Centric Developer ๐Ÿ’ฏ and while also exploring โ˜๏ธ Cloud

Working ๐Ÿ’ผ @HackerRank as a Software Development Engineer 2

๐Ÿค“ I have a keen interest in ๐Ÿค collaborating with others and empowering others to build digital solutions that solve real-world ๐ŸŒ problems. I'm a Creative Technologist who believes that the merger between Design Thinking and Digital Technologies will lead to the building of user-centric solutions that are impactful toward the betterment of business & society.

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

Daily LeetCoding Challenge

Part 4 of 6

The series is a digital notebook of the **Daily LeetCoding Challenge** from 1st July 2022. Each blog has the explanation and solution in JS for the LeetCode Challenge of the day.

Up next

LeetCode Question - 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts ๐Ÿฐ

2nd July 2022 | ๐Ÿ—“ Daily LeetCode Challenge - #2