Simple python3 solution | 817 ms - faster than 100% solutions
Complexity\n- Time complexity: O(n \cdot \log(n))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g.
# Complexity\n- Time complexity: $$O(n \\cdot \\log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` python3 []\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n        nums.sort()\n\n        not_inc = {}\n        inc = {}\n        \n        for elem in nums:            \n            inc[elem + 1] = max(\n                inc.get(elem + 1, 0),\n                1 + max(not_inc.get(elem, 0), inc.get(elem, 0)),\n            )\n            not_inc[elem] = max(\n                not_inc.get(elem, 0),\n                1 + max(not_inc.get(elem - 1, 0), inc.get(elem - 1, 0)),\n            )\n            \n        return max(\n            max(not_inc.values()),\n            max(inc.values()),\n        )\n```
['Hash Table', 'Dynamic Programming', 'Sorting', 'Python3']