-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2765.cpp
More file actions
40 lines (37 loc) · 738 Bytes
/
Copy path2765.cpp
File metadata and controls
40 lines (37 loc) · 738 Bytes
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
#include "common.h"
using namespace std;
class Solution {
public:
int alternatingSubarray(vector<int> &nums) {
int plus = 1;
int start = -1;
int max = 0;
int i;
for (i = 1; i < nums.size(); ++i) {
if (nums[i - 1] + plus == nums[i]) {
if (start == -1) {
start = i - 1;
}
plus = -plus;
} else if (start != -1) {
max = std::max(max, i - start);
start = -1;
plus = 1;
i -= 1;
}
}
if (start != -1) {
max = std::max(max, i - start);
}
if (max == 0) {
return -1;
}
return max;
}
};
int main() {
Solution s;
vector<int> v = {2,3,4,3,4};
cout << s.alternatingSubarray(v) << endl;
return 0;
}