-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path904-fruit-into-baskets.cpp
56 lines (46 loc) · 1.29 KB
/
904-fruit-into-baskets.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
55
56
class Solution {
public:
/*
[1, 2, 3, 2, 2]
p1 p2
b1 -> 2
b2 -> 3
currFruits = 4
maxFruits = 4
[3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4]
p1 p2
b1 -> 3
b2 -> 4
currFruits = 2
maxFruits = 5
*/
int totalFruit(vector<int>& tree) {
int n = tree.size();
if (n < 3) return n;
int p1 = 0;
int p2 = 0;
int prevStart = 0;
int typeB1 = tree[p1];
int typeB2 = tree[p2];
int maxFruits = 0;
for (int i = 1; i < n; i++) {
int currType = tree[i];
if (currType != typeB1 && typeB1 == typeB2) {
typeB2 = currType;
}
if (currType == typeB1 || currType == typeB2) {
p2++;
if (currType != tree[prevStart]) prevStart = i;
} else {
maxFruits = max(p2 - p1 + 1, maxFruits);
p1 = prevStart;
p2 = i;
typeB1 = tree[p1];
typeB2 = currType;
prevStart = p2;
}
}
maxFruits = max(p2 - p1 + 1, maxFruits);
return maxFruits;
}
};