Skip to content

Commit 4d395ad

Browse files
authored
Create 3147. Taking Maximum Energy From the Mystic Dungeon (#904)
2 parents 65fe47d + a9356f5 commit 4d395ad

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maximumEnergy(vector<int>& energy, int k) {
4+
int n = energy.size();
5+
6+
// Accumulate energy backwards for each k-group
7+
for (int i = 0; i < k; i++) {
8+
for (int j = n - i - 1; j >= k; j -= k) {
9+
energy[j - k] += energy[j];
10+
}
11+
}
12+
13+
// Find the maximum accumulated energy
14+
int maxa = INT_MIN;
15+
for (int i = 0; i < n; i++) {
16+
maxa = max(maxa, energy[n - i - 1]);
17+
}
18+
19+
return maxa;
20+
}
21+
};

0 commit comments

Comments
 (0)