- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution904.java
More file actions
Latest commit
executable file
·47 lines (41 loc) · 1.13 KB
/
Copy pathSolution904.java
File metadata and controls
executable file
·47 lines (41 loc) · 1.13 KB
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
importjava.util.ArrayList;
importjava.util.HashSet;
/**
* Created by slade on 2019/8/19.
*/
publicclassSolution904 {
publicinttotalFruit(int[] tree) {
if (tree.length == 0) {
return0;
}
intstart = 0;
intans = 0;
inttmp = 1;
ArrayListarr = newArrayList();
arr.add(tree[0]);
for (inti = 1; i < tree.length; i++) {
System.out.println(arr);
if (arr.contains(tree[i])) {
tmp++;
} elseif (arr.size() < 2) {
tmp++;
arr.add(tree[i]);
} else {
ans = Math.max(ans, tmp);
tmp = i - start + 1;
arr.clear();
arr.add(tree[i]);
arr.add(tree[i - 1]);
}
if (tree[i] != tree[i - 1]) {
start = i;
}
}
returnMath.max(ans, tmp);
}
publicstaticvoidmain(String[] args) {
int[] tree = {1, 0, 1, 4, 1, 4, 1, 2, 3};
Solution904s = newSolution904();
System.out.println(s.totalFruit(tree));
}
}