Java solution - Would like to know your thoughts on it
I usually post solutions in the discussion when I\'m sure that I found something interesting. This time howerver, I used an approach that seems bizzare compared
I usually post solutions in the discussion when I\'m sure that I found something interesting. This time howerver, I used an approach that seems bizzare compared to other solutions in the discussion and I would like to know your thoughts on it. Note that I tried it without the dp and it became too slow (Time limit exceeded).\n\n```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int n = nums.length;\n        Integer[] dp = new Integer[n];\n        int max = 0;\n        for (int i = 0; i < n; i++) {\n            HashSet<Integer> visited = new HashSet<> ();\n            max = Math.max(max, findLongest(nums, i, dp, visited));\n        }\n        return max;\n    }\n    \n    public int findLongest(int[] nums, int index, Integer[] dp, HashSet<Integer> visited) {\n        if (dp[index] != null) return dp[index];\n        if (visited.contains(index)) {\n            return 0;\n        } else {\n            visited.add(index);\n            dp[index] = 1 + findLongest(nums, nums[index], dp, visited);\n            return dp[index];\n        }\n    }\n}\n```
[]