Skip to content

Commit 19e05a6

Browse files
committed
740
1 parent c152e29 commit 19e05a6

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

algorithms/p740/740.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#ifndef LEETCODE_740_HPP
3+
#define LEETCODE_740_HPP
4+
5+
#include <iostream>
6+
#include <queue>
7+
#include <algorithm>
8+
#include <vector>
9+
#include <unordered_map>
10+
#include <unordered_set>
11+
#include <set>
12+
#include <numeric>
13+
#include <stack>
14+
#include <string>
15+
16+
using namespace std;
17+
18+
19+
class Solution {
20+
public:
21+
int deleteAndEarn(vector<int> &nums) {
22+
unordered_map<int, int> values;
23+
for (int num: nums) {
24+
values[num] += num;
25+
}
26+
27+
int minus2 = 0, minus1 = 0;
28+
for (int i = 0; i < 10001; ++i) {
29+
int m = max(minus1, minus2+values[i]);
30+
minus2 = minus1;
31+
minus1 = m;
32+
}
33+
return max(minus1, minus2);
34+
}
35+
};
36+
37+
38+
#endif //LEETCODE_740_HPP

algorithms/p740/740_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#include "740.hpp"
3+
#include <gtest/gtest.h>
4+
5+
TEST(p740, example) {
6+
Solution s;
7+
vector<int > arr = {2,2,3,3,3,4};
8+
ASSERT_EQ(9, s.deleteAndEarn(arr));
9+
}

0 commit comments

Comments
 (0)