MCPcopy
hub / github.com/qiyuangong/leetcode / totalFruit

Method totalFruit

java/904_Fruit_Into_Baskets.java:46–62  ·  view source on GitHub ↗
(int[] tree)

Source from the content-addressed store, hash-verified

44 }*/
45
46 public int totalFruit(int[] tree) {
47 int ans = 0, i = 0;
48 Counter count = new Counter();
49 for (int j = 0; j < tree.length; ++j) {
50 count.add(tree[j], 1);
51 while (count.size() >= 3) {
52 count.add(tree[i], -1);
53 if (count.get(tree[i]) == 0)
54 count.remove(tree[i]);
55 i++;
56 }
57
58 ans = Math.max(ans, j - i + 1);
59 }
60
61 return ans;
62 }
63}
64
65class Counter extends HashMap<Integer, Integer> {

Callers

nothing calls this directly

Calls 4

addMethod · 0.95
getMethod · 0.95
maxMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected