🎉 One-stop destination for all your technical interview Preparation 🎉
You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:
Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1. Return the maximum number of points you can earn by applying the above operation some number of times.
class Solution {
public:
int deleteAndEarn(vector<int>& nums)
{
vector<int> sums(10001, 0);
int n = nums.size();
for (int i = 0; i < n; i++) {
sums[nums[i]] += nums[i];
}
// house robber code
vector<int> dp(10001, 0);
dp[0] = sums[0];
dp[1] = max(sums[0], sums[1]);
for (int i = 2; i < 10001; i++) {
dp[i] = max(sums[i] + dp[i - 2], dp[i - 1]);
}
return dp[10000];
}
};
class Solution {
public:
int deleteAndEarn(vector<int>& nums)
{
vector<int> sums(10001, 0);
int n = nums.size();
for (int i = 0; i < n; i++) {
sums[nums[i]] += nums[i];
}
// house rober code
int first = sums[0];
int second = max(sums[0], sums[1]);
for (int i = 2; i < 10001; i++) {
int curr = max(sums[i] + first, second);
first = second;
second = curr;
}
return second;
}
};