<p>A <strong>wiggle sequence</strong> 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.</p>

<ul>
	<li>For example, <code>[1, 7, 4, 9, 2, 5]</code> is a <strong>wiggle sequence</strong> because the differences <code>(6, -3, 5, -7, 3)</code> alternate between positive and negative.</li>
	<li>In contrast, <code>[1, 4, 7, 2, 5]</code> and <code>[1, 7, 4, 5, 5]</code> 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.</li>
</ul>

<p>A <strong>subsequence</strong> is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.</p>

<p>Given an integer array <code>nums</code>, return <em>the length of the longest <strong>wiggle subsequence</strong> of </em><code>nums</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,7,4,9,2,5]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,17,5,10,13,15,10,5,16,8]
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are several subsequences that achieve this length.
One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,6,7,8,9]
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> Could you solve this in <code>O(n)</code> time?</p>

class Solution {
public:

    int rec(vector<int>nums, int flag, int prev,int i,vector<vector<vector<int>>>& t) //flag =2(decrease) 1(increase)
    {   if(t[flag][prev][i] != -1){return t[flag][prev][i];}
        if(i==nums.size())
        {
            t[flag][prev][i]=0;
            return 0;
        }
        if(flag==0)
        {
            int a = rec(nums,1,nums[i],i+1,t)+1;
            int b = rec(nums,2,nums[i],i+1,t)+1;
            int c = rec(nums,0,0,i+1,t);

           t[flag][prev][i]=max(a,max(b,c));
           return  t[flag][prev][i];
        }
        else if(flag==1)
        {   int a = INT_MIN;
            if(nums[i] > prev){a = rec(nums,2,nums[i],i+1,t)+1;}
            int b = rec(nums,1,prev,i+1,t);
           t[flag][prev][i]=max(a,b);
           return  t[flag][prev][i];
        }
        else if(flag==2)
        {
            int a = INT_MIN;
            if(nums[i]<prev){a = rec(nums,1,nums[i],i+1,t)+1;}
            int b = rec(nums,2,prev,i+1,t);
            t[flag][prev][i]=max(a,b);
            return  t[flag][prev][i];
        }
        return 0;
    }

    int greedy(vector<int>nums)
    {
        int size = nums.size();
        int top = 1;
        int bottom = 1;
        for(int i=1; i<size; ++i){
                 if(nums[i]>nums[i-1]) top = bottom + 1;
            else if(nums[i]<nums[i-1]) bottom = top + 1;
        }
        return max(top,bottom);
    }
    int wiggleMaxLength(vector<int>& nums) {
        //int t[3][max_element(nums.begin(),nums.end())][nums.size()];
        auto m = max_element(nums.begin(),nums.end());
        int max_ele = *m;
        vector<vector<vector<int>>> t(3, vector<vector<int>>(max_ele + 2, vector<int>(nums.size() + 1, -1)));
        return greedy(nums);
    }
};
35296