Skip to content

Commit 6814343

Browse files
Taking maximum energy from mystic dungeon
1 parent d7b1b4c commit 6814343

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
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.
3+
4+
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.
5+
6+
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.
7+
8+
You are given an array energy and an integer k. Return the maximum possible energy you can gain.
9+
10+
Note that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
11+
'''
12+
13+
def maximumEnergy(energy, k):
14+
n = len(energy)
15+
dp = [0]*n
16+
17+
for i in range(n-1, -1, -1):
18+
if i+k < n:
19+
dp[i] = energy[i] + dp[i+k]
20+
else:
21+
dp[i] = energy[i]
22+
23+
print(dp)
24+
return max(dp)
25+
26+
27+
ans = maximumEnergy([5,2,-10,-5,1], 3)
28+
print(ans)

0 commit comments

Comments
 (0)