feat: added WA and TLE solutions to delete-and-earn problem, also wrote the tutorial file

This commit is contained in:
2025-11-11 11:45:03 -03:00
parent 61a63e62df
commit 41bfc36185
7 changed files with 198 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int deleteAndEarn(vector<int> &nums)
{
int sum = 0;
map<int, int> quantity;
for (int n : nums)
{
sum += n;
quantity[n]++;
}
int maxSum = INT_MIN;
for (const auto &pair : quantity)
{
int currentSum = sum, n = pair.first;
if (quantity.count(n - 1))
currentSum -= quantity[n - 1] * (n - 1);
if (quantity.count(n + 1))
currentSum -= quantity[n + 1] * (n + 1);
maxSum = max(maxSum, currentSum);
}
return maxSum;
}
int main()
{
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++)
cin >> nums[i];
cout << deleteAndEarn(nums) << endl;
return 0;
}