feat: added WA and TLE solutions to delete-and-earn problem, also wrote the tutorial file
This commit is contained in:
37
delete-and-earn/src/WA.cpp
Normal file
37
delete-and-earn/src/WA.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user