-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
43 lines (39 loc) · 850 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int rob(unordered_map<int, int> &nums)
{
size_t n = nums.size();
int a0 = nums[0];
if (n == 1)
return a0;
int a1 = max(nums[0], nums[1]);
if (n == 2)
return a1;
int temp;
for (int i = 0; i < (int)(n - 2); i++)
{
temp = max(a0 + nums[2 + i], a1);
a0 = a1;
a1 = temp;
}
return a1;
}
int deleteAndEarn(vector<int> &nums)
{
// vector<int> tb(10001);
unordered_map<int, int> tb;
for (size_t i = 0; i < nums.size(); i++)
tb[nums[i]] += nums[i];
return rob(tb);
}
int main()
{
vector<int> vec = {3, 4, 2};
cout << deleteAndEarn(vec) << "\n";
return 0;
}