(int[] tree)
| 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 | |
| 65 | class Counter extends HashMap<Integer, Integer> { |