-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
376.cpp
50 lines (48 loc) · 1.52 KB
/
376.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
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if(nums.size()<2)
return nums.size();
int up[nums.size()];
int down[nums.size()];
up[0] = down[0] = 1;
for(int i=1;i<nums.size();i++){
if(nums[i]>nums[i-1]){
up[i] = max(up[i-1],down[i-1]+1);
down[i] = down[i-1];
}
else if(nums[i]<nums[i-1]){
down[i]=max(down[i-1],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]);
}
};
__________________________________________________________________________________________________
sample 8444 kb submission
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
// Solution 1: O(n) DP solution.
int l = nums.size(), down = 1, up = 1;
if(l < 2)
return l;
for(int i=1; i < l; i++){
if(nums[i] > nums[i-1]){
up = down + 1;
}
else if(nums[i] < nums[i-1]){
down = up + 1;
} // else, up and down remain the same.
}
return max(up, down);
}
};
__________________________________________________________________________________________________