forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (42 loc) · 1.3 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Memory Search
/// This problem can be transform into House Robber Problem
/// See: https://leetcode.com/problems/house-robber/description/
///
/// Time Complexity: O(maxNum)
/// Space Complexity: O(maxNum)
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
vector<int> scores(10001, 0);
int maxNum = 0;
for(int num: nums){
scores[num] += num;
maxNum = max(num, maxNum);
}
vector<int> dp(maxNum + 1, -1);
return findResult(scores, 0, maxNum, dp);
}
private:
int findResult(const vector<int>& scores,
int index, int lastIndex, vector<int>& dp){
if(index > lastIndex)
return 0;
if(dp[index] != -1)
return dp[index];
if(index == lastIndex)
return dp[index] = scores[index];
return dp[index] = max(findResult(scores, index + 1, lastIndex, dp),
scores[index] + findResult(scores, index + 2, lastIndex, dp));
}
};
int main() {
vector<int> nums1 = {3, 4, 2};
cout << Solution().deleteAndEarn(nums1) << endl;
vector<int> nums2 = {2, 2, 3, 3, 3, 4};
cout << Solution().deleteAndEarn(nums2) << endl;
return 0;
}