Skip to content

Commit 1e46ccc

Browse files
authored
Create 3186. Maximum Total Damage With Spell Casting
1 parent 4d395ad commit 1e46ccc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
long long dp[100005];
4+
long long solve(vector<pair<int,int>>&nums,int idx){
5+
if(idx>=nums.size()) return 0;
6+
7+
if(dp[idx]!=-1) return dp[idx];
8+
9+
long long take = 0,skip = 0;
10+
11+
int j = idx+1;
12+
while(j<nums.size()){
13+
if(nums[j].first!=nums[idx].first+1 && nums[j].first!=nums[idx].first+2) break;
14+
j++;
15+
}
16+
take = 1LL*nums[idx].first*nums[idx].second + solve(nums,j);
17+
skip = solve(nums,idx+1);
18+
19+
return dp[idx] = max(take,skip);
20+
}
21+
long long maximumTotalDamage(vector<int>& power) {
22+
23+
int n = power.size();
24+
map<int,int>mp;
25+
for(auto &it:power) mp[it]++;
26+
27+
vector<pair<int,int>>nums;
28+
for(auto &it:mp){
29+
nums.push_back({it.first,it.second});
30+
}
31+
32+
sort(nums.begin(),nums.end());
33+
34+
memset(dp,-1,sizeof(dp));
35+
36+
return solve(nums,0);
37+
}
38+
};

0 commit comments

Comments
 (0)