【动态规划】删除并获得点数
题目链接:https://leetcode.cn/problems/delete-and-earn/description/
class Solution {
public:
int deleteAndEarn(vector& nums)
{
/时间复杂度O(n),空间复杂度O(1)/
const int N = 10001;
// 预处理
int arr[N] = { 0 };
for (int x : nums) arr[x] += x;
// 在arr数组中来一次"打家劫舍" // 创建dp表 vector<int> f(N); auto g = f; // 初始化 // 填表 for (int i = 1; i < N; ++i) { f[i] = g[i - 1] + arr[i]; g[i] = max(f[i - 1], g[i - 1]); } // 返回值 return max(f[N - 1], g[N - 1]); }};
