-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSolution904.java
executable file
·47 lines (41 loc) · 1.13 KB
/
Solution904.java
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
import java.util.ArrayList;
import java.util.HashSet;
/**
* Created by slade on 2019/8/19.
*/
public class Solution904 {
public int totalFruit(int[] tree) {
if (tree.length == 0) {
return 0;
}
int start = 0;
int ans = 0;
int tmp = 1;
ArrayList arr = new ArrayList();
arr.add(tree[0]);
for (int i = 1; i < tree.length; i++) {
System.out.println(arr);
if (arr.contains(tree[i])) {
tmp++;
} else if (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;
}
}
return Math.max(ans, tmp);
}
public static void main(String[] args) {
int[] tree = {1, 0, 1, 4, 1, 4, 1, 2, 3};
Solution904 s = new Solution904();
System.out.println(s.totalFruit(tree));
}
}