-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_376_WiggleSubsequence.cpp
54 lines (43 loc) · 1.35 KB
/
LC_376_WiggleSubsequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
https://leetcode.com/problems/wiggle-subsequence/
376. Wiggle Subsequence
https://practice.geeksforgeeks.org/problems/longest-alternating-subsequence5951/1/#
https://binarysearch.com/problems/Longest-Sign-Alternating-Subsequence
*/
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
vector<int> up(nums.size(),1);
vector<int> down(nums.size(),1);
for(int i=1; i<nums.size(); i++)
{
if(nums[i-1] < nums[i])
{
up[i] = down[i-1] + 1;
down[i]= down[i-1];
}
else if(nums[i-1] > nums[i])
{
down[i] = up[i-1] + 1;
up[i]= up[i-1];
}
else
{
down[i]= down[i-1];
up[i]= up[i-1];
}
}
return max(up[nums.size()-1], down[nums.size()-1]);
// O(n)
// int up=1;
// int down=1;
// for(int i=1; i<nums.size(); i++)
// {
// if(nums[i-1] < nums[i]) // current num is greater than previous
// up = down+1;
// else if(nums[i-1] > nums[i]) // current num is lesser than previous
// down = up+1;
// }
// return max(up, down);
}// end
};