Created
October 10, 2025 18:56
-
-
Save tatsuyax25/8a2a34d128671537e7940127e608c6df to your computer and use it in GitHub Desktop.
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 e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} energy | |
| * @param {number} k | |
| * @return {number} | |
| */ | |
| var maximumEnergy = function(energy, k) { | |
| // Initialize the answer to a very low number to ensure any valid energy path will be higher | |
| let ans = -1e99; | |
| // Try starting from each index i in the range [0, k) | |
| // These are the only starting points that can reach the end via i + k jumps | |
| for (let i = 0; i < k; i++) { | |
| // Start with the energy at index i | |
| let cur = energy[i]; | |
| // Set the next index to jump to | |
| let curI = i + k; | |
| // Continue jumping by k until we go out of bounds | |
| while (curI < energy.length) { | |
| // Either continue accumulating energy or restart from current magician | |
| // This handles negative energy dips and resets if restarting is better | |
| cur = Math.max(cur + energy[curI], energy[curI]); | |
| // Move to the next magician in the teleport sequence | |
| curI += k; | |
| } | |
| // Update the maximum energy found so far | |
| ans = Math.max(cur, ans); | |
| } | |
| // Return the best energy total across all valid paths | |
| return ans; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment