Java Sliding Window + Trie as Hash 86ms beat 100%
Algorithm:\n(1) Using Sliding Window to get the longest valid sub array, O(N)\n(2)  Insert into Trie and mark all the way isEnd == true, O(N)\n(3) Total time is
Algorithm:\n(1) Using Sliding Window to get the longest valid sub array, O(N)\n(2)  Insert into Trie and mark all the way isEnd == true, O(N)\n(3) Total time is O(N^2)\n\n```\nclass TrieNode {\n    public int val;\n    public boolean isEnd;\n    public Map<Integer, TrieNode> children;\n    public TrieNode(int val) {\n        this.val = val;\n        this.isEnd = false;\n        this.children = new HashMap<>();\n    }\n}\nclass Trie {\n    public TrieNode root;\n    public int totalCount;\n    public Trie() {\n        this.root = new TrieNode(-1);\n    }\n    public void insert(int[] nums, int left, int right) {\n        TrieNode cur = this.root;\n        for(int i = left; i <= right; i++){\n            \n            int cn = nums[i];\n            if (!cur.children.containsKey(cn)){\n                cur.children.put(cn, new TrieNode(cn));\n            }\n            cur = cur.children.get(cn);\n            if (!cur.isEnd) {\n                totalCount++;\n                cur.isEnd = true;\n            }\n        }\n    }\n    public int getCount() {\n        return totalCount;\n    }\n}\nclass Solution {\n    public int countDistinct(int[] nums, int k, int p) {\n        int count = 0;\n        int n = nums.length;\n        Trie curTrie = new Trie();\n        int left = 0;\n        int right = 0;\n        while (left < n) {\n            while (right < n) {\n                if (nums[right] % p == 0) {\n                    count++;\n                }\n                if (count == k + 1) {\n                    count--;\n                    break;\n                }\n                right++;\n            }\n            curTrie.insert(nums, left, right - 1);\n            if (nums[left] % p == 0) {\n                count--;\n            }\n            left++;\n        }\n        \n        return curTrie.getCount();\n    }\n}\n```
['Trie', 'Sliding Window', 'Java']