-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path904.cpp
More file actions
27 lines (27 loc) · 788 Bytes
/
Copy path904.cpp
File metadata and controls
27 lines (27 loc) · 788 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
class Solution {
public:
int totalFruit(vector<int> &tree) {
time_t begin = clock();
int size = tree.size();
int total = 0;
for (int i = 0; i < size; ++i) {
if (i > 0 && tree[i] == tree[i - 1])
continue;
int a = tree[i], b = -1;
int current = 1, j = i + 1;
for (; j < size; ++j) {
if (b != -1 && (tree[j] != a && tree[j] != b))
break;
if (b == -1 && tree[j] != a)
b = tree[j];
++current;
}
total = max(total, current);
if (j == size)
break;
}
time_t end = clock();
cout << end - begin << endl;
return total;
}
};