-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path376.cpp
More file actions
21 lines (21 loc) · 690 Bytes
/
376.cpp
File metadata and controls
21 lines (21 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//376
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if(nums.size() < 2) return nums.size();
if(nums.size() == 2) return 1+(nums[0] != nums[1]);
vector<int> tmp;
tmp.push_back(nums[0]);
for(int i = 1;i < nums.size();i++)
if(nums[i] != nums[i-1]) tmp.push_back(nums[i]);
if(tmp.size() < 3) return tmp.size();
int cnt = 0;
for(int i = 1; i < nums.size()-1;i++) {
int dl = nums[i] - nums[i-1];
int dr = nums[i+1] - nums[i];
if(dl*dr < 0) cnt++;
}
if(cnt == 0) return 1+(tmp.front() != tmp.back());
return cnt+2;
}
};