-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFruitIntoBaskets.java
More file actions
53 lines (50 loc) · 1.52 KB
/
Copy pathFruitIntoBaskets.java
File metadata and controls
53 lines (50 loc) · 1.52 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
48
49
50
51
52
53
package google;
import java.util.Arrays;
public class FruitIntoBaskets {
public int totalFruit(int[] tree) {
int i = 0, k = 0, j =0, max = Integer.MIN_VALUE, temp;
int[][] map = new int[2][2];
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
map[i][j] = 0;
}
}
map[0][0] = -1;
map[1][0] = -1;
i=0;
for (k = 0;k<tree.length;k++){
if (map[0][0] == tree[k]) {
map[0][1]++;
} else if (map[1][0] == tree[k]) {
map[1][1]++;
} else if (map[0][0] == -1) {
map[0][0] = tree[k];
map[0][1]++;
} else if (map[1][0] == -1) {
map[1][0] = tree[k];
map[1][1]++;
} else {
temp = tree[k-1];
if(map[0][0] == tree[k-1]){
map[1][0] = tree[k];
map[1][1] = 1;
map[0][1] = 0;
for(j=k-1;j>=i && temp==tree[j];j--){
map[0][1]++;
}
} else{
map[0][0] = tree[k];
map[0][1] = 1;
map[1][1] = 0;
for(j=k-1;j>=i && temp==tree[j];j--){
map[1][1]++;
}
}
}
if(max<map[0][1] + map[1][1]) {
max = map[0][1] + map[1][1];
}
}
return max;
}
}