3147. Taking Maximum Energy From the Mystic Dungeon LeetCode Solution
In this guide, you will get 3147. Taking Maximum Energy From the Mystic Dungeon LeetCode Solution with the best time and space complexity. The solution to Taking Maximum Energy From the Mystic Dungeon problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Taking Maximum Energy From the Mystic Dungeon solution in C++
- Taking Maximum Energy From the Mystic Dungeon solution in Java
- Taking Maximum Energy From the Mystic Dungeon solution in Python
- Additional Resources

Problem Statement of Taking Maximum Energy From the Mystic Dungeon
In a mystic dungeon, n magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you.
You have been cursed in such a way that after absorbing energy from magician i, you will be instantly transported to magician (i + k). This process will be repeated until you reach the magician where (i + k) does not exist.
In other words, you will choose a starting point and then teleport with k jumps until you reach the end of the magicians’ sequence, absorbing all the energy during the journey.
You are given an array energy and an integer k. Return the maximum possible energy you can gain.
Note that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
Example 1:
Input: energy = [5,2,-10,-5,1], k = 3
Output: 3
Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
Example 2:
Input: energy = [-2,-3,-1], k = 2
Output: -1
Explanation: We can gain a total energy of -1 by starting from magician 2.
Constraints:
1 <= energy.length <= 105
-1000 <= energy[i] <= 1000
1 <= k <= energy.length – 1
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3147. Taking Maximum Energy From the Mystic Dungeon LeetCode Solution in C++
class Solution {
public:
int maximumEnergy(vector<int>& energy, int k) {
vector<int> dp(energy);
for (int i = energy.size() - 1 - k; i >= 0; --i)
dp[i] += dp[i + k];
return ranges::max(dp);
}
};
/* code provided by PROGIEZ */
3147. Taking Maximum Energy From the Mystic Dungeon LeetCode Solution in Java
class Solution {
public int maximumEnergy(int[] energy, int k) {
int[] dp = energy.clone();
for (int i = energy.length - 1 - k; i >= 0; --i)
dp[i] += dp[i + k];
return Arrays.stream(dp).max().getAsInt();
}
}
// code provided by PROGIEZ
3147. Taking Maximum Energy From the Mystic Dungeon LeetCode Solution in Python
class Solution:
def maximumEnergy(self, energy: list[int], k: int) -> int:
# dp[i] := the sum of energy starting at i
dp = energy.copy()
for i in range(len(energy) - 1 - k, -1, -1):
dp[i] += dp[i + k]
return max(dp)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.