-
Notifications
You must be signed in to change notification settings - Fork 46
/
_904.java
37 lines (30 loc) · 897 Bytes
/
_904.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
import java.util.HashMap;
import java.util.Map;
/**
* LeetCode 904 - Fruit Into Baskets
*
* Sliding-window
*/
public class _904 {
public int totalFruit(int[] tree) {
int res = 0;
Map<Integer, Integer> baskets = new HashMap<>();
int j = 0;
for (int i = 0; i < tree.length; i++) {
while (j < i && baskets.size() == 2 && !baskets.containsKey(tree[i])) {
baskets.put(tree[j], baskets.get(tree[j]) - 1);
if (baskets.get(tree[j]) == 0) {
baskets.remove(tree[j]);
}
j++;
}
baskets.put(tree[i], baskets.getOrDefault(tree[i], 0) + 1);
int sum = 0;
for (int fruitCount : baskets.values()) {
sum += fruitCount;
}
res = Math.max(res, sum);
}
return res;
}
}