March LeetCoding Challenge 2021 — Day 18: Wiggle Subsequence
Tags: computerscience, codenewbie, programming, algorithms
Published: 2021-03-18 15:24:31



Today, we will solve the 18th problem of the March LeetCoding Challenge.


## Problem Statement

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

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 fewer than two elements is trivially a wiggle sequence.

* For example, [1, 7, 4, 9, 2, 5] is a **wiggle sequence** because the differences (6, -3, 5, -7, 3) are alternately positive and negative.

* In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.

A **subsequence** is obtained by deleting some elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.

**Example 1:**

    **Input:** nums = [1,7,4,9,2,5]
    **Output:** 6
    **Explanation:** The entire sequence is a wiggle sequence.

**Example 2:**

    **Input:** nums = [1,17,5,10,13,15,10,5,16,8]
    **Output:** 7
    **Explanation:** There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].

**Example 3:**

    **Input:** nums = [1,2,3,4,5,6,7,8,9]
    **Output:** 2

## Solution

With this problem, we have to find the longest wiggle subsequence. Wiggle subsequence means that the difference between successive elements is alternating between positive and negative.

For solving this problem we will use dynamic programming. We will go through two approaches, one with O(n²) and another one with O(n).

**Approach 1:** We will use two arrays up and down. up array refers to the length of the longest wiggle subsequence where the ending element is a rising wiggle. Similarly, the down array refers to the length of the longest wiggle subsequence where the ending element is a falling wiggle.

We will update the up[i] when we find a rising wiggle at index i. But there is a catch. We have to consider the previous wiggle subsequence which ends with a falling wiggle (because we need to make sure that the subsequence overall is wiggle). We follow the same in the case of down[i].

The code is given below.
{% gist https://gist.github.com/sksaikia/e4a7882a8f40b93f490d00af19fa5f3f.js %}
Here we are using two loops. To check the longest wiggle subsequence at index i , we check for each element from 0 to i-1.

Time complexity: O(n²), n is the length of the array

Space complexity: O(n), n is the length of the array

**Approach 2:** With this approach, we only use a single loop and two arrays named up and down. Here, we have 3 cases.

* When nums[i]>nums[i-1]:it means it wiggles up. So the element before it must be in down position. Therefore, up[i] = down[i-1]+1 and down[i] will be same as down[i-1].

* When nums[i]<nums[i-1]: it means the wiggles down, the element before it must be in up position. Therefore down[i] = 1+up[i-1] and up[i] will be same as up[i-1].

* If nums[i]=nums[i-1],then up[i] will be up[i-1] and down[i] will be down[i-1].

![](https://cdn-images-1.medium.com/max/2400/1*MgzyW08XYtsfyoLk6ku8Qw.gif)

The code is given below.
{% gist https://gist.github.com/sksaikia/72d2fcfdfd075631e81b8367bf6403f5.js %}
Time complexity: O(n), n is the length of the array

Space complexity: O(n), n is the length of the array

The code can be found here
{%github sksaikia/Leetcode %}

Check out my other posts on March LeetCoding Challenge 2021.

 1. [March LeetCoding Challenge — Day 1 — Distribute Candies](https://medium.com/dev-genius/march-leetcoding-challenge-2021-problem-1-distribute-candies-f37f66ea7ee9)

 2. [March LeetCoding Challenge — Day 2 — Set Mismatch](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-2-set-mismatch-4abd5ee491c9)

 3. [March LeetCoding Challenge — Day 3 — Missing Number](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-3-missing-number-ae8ee45a58cb)

 4. [March LeetCoding Challenge — Day 4 — Intersection of Two Linked Lists](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-4-intersection-of-two-linked-lists-a775449b5563)

 5. [March LeetCoding Challenge — Day 5 — Average of Levels in Binary Tree](https://link.medium.com/sC9L595opeb)

 6. [March LeetCoding Challenge — Day 6 — Short Encoding of Words](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-6-short-encoding-of-words-7fed4bfae557)

 7. [March LeetCoding Challenge — Day 7 — Design HashMap](https://levelup.gitconnected.com/leetcode-706-design-hashmap-march-leetcoding-challenge-2021-fdae1a4adbc)

 8. [March LeetCoding Challenge — Day 8 — Remove Palindromic Subsequences](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-8-remove-palindromic-subsequences-12b037705722)

 9. [March LeetCoding Challenge — Day 10 — Integer to Roman](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-10-integer-to-roman-76caa87a0e07)

 10. [March LeetCoding Challenge — Day 12 — Check If a String Contains All Binary Codes of Size K](https://medium.com/dev-genius/march-leetcoding-challenge-2021-day-12-check-if-a-string-contains-all-binary-codes-of-size-k-48f1fed4d9b9)

 11. [March LeetCoding Challenge — Day 14 — Swapping Nodes in a Linked List](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-14-swapping-nodes-in-a-linked-list-a540785c816f)

 12. [March LeetCoding Challenge — Day 15 — Encode and Decode TinyURL](https://medium.com/dev-genius/march-leetcoding-challenge-2021-day-15-encode-and-decode-tinyurl-bfeeba44308e)