Skip to content

Commit b6ae010

Browse files
CPP solution for Leetcode 3147 — Taking Maximum Energy From the Mystic Dungeon
1 parent c477514 commit b6ae010

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

leetcode 3147.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int maximumEnergy(vector<int>& energy, int k) {
7+
int n = energy.size();
8+
vector<int> dp(n, 0);
9+
int ans = INT_MIN;
10+
11+
for (int i = n - 1; i >= 0; i--) {
12+
dp[i] = energy[i];
13+
if (i + k < n)
14+
dp[i] += dp[i + k];
15+
ans = max(ans, dp[i]);
16+
}
17+
18+
return ans;
19+
}
20+
};

0 commit comments

Comments
 (0)