Clean DP memorization without TLE
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int maxSelectedElements(int[] nums) {\n        // sort to find longest consecutive sequence\n        Arrays.sort(nums);\n        int n = nums.length;\n        // cur index_prev value, length of sequence\n        Map<String, Integer> memo = new HashMap<>();\n\n        int res = helper(nums, 0, -1, memo);\n        return res;\n    }\n\n    private int helper(int[] nums, int i, int prev, Map<String, Integer> memo) {\n\n        // base case\n        if (i == nums.length) {\n            return 0;\n        }\n\n        String key = i + "-" + prev;\n        if (memo.containsKey(key))\n            return memo.get(key);\n\n        int take = 0;\n        // sequence start or already 1 greater than prev value so we increment curr by 0\n        if (prev == -1 || prev + 1 == nums[i])\n            take = 1 + helper(nums, i + 1, nums[i], memo);\n\n        // if first num in sequence or equals to prev value so we increment curr by 1\n        if (prev == -1 || prev == nums[i]) {\n            take = Math.max(take, 1 + helper(nums, i + 1, nums[i] + 1, memo));\n        }\n        // start sequence from later index or if prev is more we can\'t take this index so skip.\n        if (prev == -1 || prev > nums[i])\n            take = Math.max(take, 0 + helper(nums, i + 1, prev, memo));\n\n        memo.put(key, take);\n        return take;\n    }\n}\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'Sorting', 'Java']