My Solutions
1. Use the DP I\n\n/**\n * Time Complexity: O(n * log(n))\n * Space Complexity: O(n)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n p
**1. Use the DP `I`**\n```\n/**\n * Time Complexity: O(n * log(n))\n * Space Complexity: O(n)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n public:\n  int maxSelectedElements(vector<int> &nums) {\n    constexpr int deltas[] = {-1, 0, 1};\n    const int n = static_cast<int>(nums.size());\n    sort(nums.begin(), nums.end());\n    vector<int> values;\n    int index_mapping[n];\n    memset(index_mapping, -1, sizeof(index_mapping));\n    for (int i = 0; i < n; ++i) {\n      if (i > 0 && nums[i] == nums[i - 1]) {\n        index_mapping[i] = index_mapping[i - 1];\n        continue;\n      }\n      \n      for (const int delta : deltas) {\n        const int inserted = nums[i] + delta;\n        if (values.empty() || values.back() < inserted) {\n          values.emplace_back(inserted);\n        }\n      }\n      \n      index_mapping[i] = static_cast<int>(values.size()) - 2;\n    }\n    \n    const int n_values = static_cast<int>(values.size());\n    int dp[n_values];\n    memset(dp, 0, sizeof(dp));\n    for (int i = 0; i < n; ++i) {\n      /**\n       * `i_previous` is the index of the value (`nums[i]` - 1) in the vector `values`\n       * `i_current` is the index of the value `nums[i]` in the vector `values`\n       * `i_next` is the index of the value (`nums[i]` + 1) in the vector `values`\n       */\n      const int i_current = index_mapping[i];\n      const int i_previous = i_current - 1;\n      const int i_next = i_current + 1;\n      dp[i_next] = dp[i_current] + 1;\n      dp[i_current] = dp[i_previous] + 1;\n    }\n    \n    return *max_element(dp, dp + n_values);\n  }\n};\n```\n**2. Use the DP `II`**\n```\n/**\n * Time Complexity: O(n * log(n))\n * Space Complexity: O(n)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n public:\n  int maxSelectedElements(vector<int> &nums) {\n    constexpr int options = 2;\n    constexpr int unchanged_i = 0;\n    constexpr int increased_i = 1;\n    const int n = static_cast<int>(nums.size());\n    sort(nums.begin(), nums.end());\n    int dp[options][n];\n    for (int i = 0; i < n; ++i) {\n      dp[unchanged_i][i] = 1;\n      dp[increased_i][i] = 1;\n      \n      for (int option = 0; option < options; ++option) {\n        for (int previous_option = 0; previous_option < options; ++previous_option) {\n          const int previous_num = nums[i] + option - 1 - previous_option;\n          auto ub = upper_bound(nums.begin(), nums.begin() + i, previous_num);\n          if (ub != nums.begin() && *prev(ub) == previous_num) {\n            const int previous_index = static_cast<int>(prev(ub) - nums.begin());\n            dp[option][i] = max(dp[option][i], dp[previous_option][previous_index] + 1);\n          }\n        }\n      }\n    }\n    return max(*max_element(dp[unchanged_i], dp[unchanged_i] + n),\n               *max_element(dp[increased_i], dp[increased_i] + n));\n  }\n};\n```
[]