@@ -0,0 +1,1193 @@
+package com.flang.twopointer;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Apublic class FruitIntoBaskets %7B%0A%0A    public static int totalFruit(int%5B%5D tree) %7B%0A        if (tree.length == 0) return 0;%0A        int max = 0;%0A        int left = 0;%0A        // use hashmap to track how many types of fruits we can have %0A        Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        for (int right = 0; right %3C tree.length; right++) %7B%0A            map.put(tree%5Bright%5D, map.getOrDefault(tree%5Bright%5D, 0) + 1);%0A            // if the map size bigger than 2, we know there are 3 type of fruits in the map%0A            // so we need to remove the element start from left pointer%0A            while (map.size() %3E 2) %7B%0A                if (map.get(tree%5Bleft%5D) == 1) %7B%0A                    map.remove(tree%5Bleft%5D);%0A                %7D else %7B%0A                    map.put(tree%5Bleft%5D, map.get(tree%5Bleft%5D) - 1);%0A                %7D%0A                left++;%0A            %7D%0A            max = Math.max(max, right - left + 1);%0A        %7D%0A        return max;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        int rst = FruitIntoBaskets.totalFruit(new int%5B%5D%7B3,3,3,1,2,1,1,2,3,3,4%7D);%0A        System.out.println(rst);%0A    %7D%0A%7D%0A

Create FruitIntoBaskets.java