Problem#
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.
Examples#
Example 1#
1
2
3
4
5
6
7
|
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#
1
2
3
4
5
6
|
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 <= 10^5
-1000 <= energy[i] <= 1000
1 <= k <= energy.length - 1
Solution#
Approach#
For each possible starting index i
(from 0
to k-1
), you can only visit indices of the form i, i+k, i+2k, ...
. For each such sequence, sum the energies along the way and keep the maximum among all possible starting indices. This can be efficiently computed using dynamic programming from the end of the array.
Code#
C++#
1
2
3
4
5
6
7
8
9
10
11
12
13
|
int maximumEnergy(vector<int>& energy, int k) {
int n = energy.size();
vector<int> dp(n);
int ans = INT_MIN;
for (int i = n - 1; i >= 0; --i) {
if (i + k < n)
dp[i] = energy[i] + dp[i + k];
else
dp[i] = energy[i];
ans = max(ans, dp[i]);
}
return ans;
}
|
Java#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public int maximumEnergy(int[] energy, int k) {
int n = energy.length;
int[] dp = new int[n];
int ans = Integer.MIN_VALUE;
for (int i = n - 1; i >= 0; --i) {
if (i + k < n) {
dp[i] = energy[i] + dp[i + k];
} else {
dp[i] = energy[i];
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
|
Kotlin#
1
2
3
4
5
6
7
8
9
10
11
|
fun maximumEnergy(energy: IntArray, k: Int): Int {
val n = energy.size
val dp = IntArray(n)
var ans = Int.MIN_VALUE
for (i in n - 1 downTo 0) {
dp[i] = energy[i]
if (i + k < n) dp[i] += dp[i + k]
ans = maxOf(ans, dp[i])
}
return ans
}
|
Python#
1
2
3
4
5
6
7
8
9
10
11
|
def maximumEnergy(energy, k):
n = len(energy)
dp = [float('-inf')] * n
ans = float('-inf')
for i in range(n-1, -1, -1):
if i + k < n:
dp[i] = energy[i] + dp[i + k]
else:
dp[i] = energy[i]
ans = max(ans, dp[i])
return ans
|
Rust#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
pub fn maximum_energy(energy: Vec<i32>, k: i32) -> i32 {
let n = energy.len();
let k = k as usize;
let mut dp = vec![0; n];
let mut ans = i32::MIN;
for i in (0..n).rev() {
dp[i] = energy[i];
if i + k < n {
dp[i] += dp[i + k];
}
ans = ans.max(dp[i]);
}
ans
}
|
TypeScript#
1
2
3
4
5
6
7
8
9
10
11
|
function maximumEnergy(energy: number[], k: number): number {
const n = energy.length;
const dp = new Array(n).fill(0);
let ans = -Infinity;
for (let i = n - 1; i >= 0; --i) {
dp[i] = energy[i];
if (i + k < n) dp[i] += dp[i + k];
ans = Math.max(ans, dp[i]);
}
return ans;
}
|
Explanation#
For each possible starting index, we sum the energies at every k-th step using dynamic programming from the end. The answer is the maximum sum among all possible starting indices.
Complexity#
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)