1 min read
My LeetCode Daily Log
Keeping track of my progress helps me spot patterns, measure consistency. Each entry expands to reveal the approach, key takeaways, and a reference implementation. I update the log whenever I solve something new. The main reason I’m sharing these solutions is to ensure my own consistency and to take notes.
Matching entries update live as you type.
No problems match that search. Try a different keyword.
2025
10 Oct 2025 Medium Dynamic Programming Array 3147. Taking Maximum Energy From the Mystic Dungeon
You can start from any magician and must jump forward by k positions after each step, collecting positive or negative energy. The goal is to maximize the total collected energy.
Approach
- Let dp[i] represent the total energy collected starting from magician i.
- Compute dp[i] = energy[i] + dp[i + k] if i + k is within bounds; otherwise dp[i] = energy[i].
- Iterate backward (from right to left) so dp[i + k] is already known.
- Track the maximum value among all dp[i] as the final result.
Language: Go
Notes
Initialize result with a very small number (-1 << 31) to handle negative-only arrays. This ensures the first computed value replaces it correctly.
Reference Solution
func maximumEnergy(energy []int, k int) int {
n := len(energy)
dp := make([]int, n)
result := -1 << 31 // start with the smallest 32-bit integer
for i := n - 1; i >= 0; i-- {
next := 0
if i + k < n {
next = dp[i + k]
}
dp[i] = energy[i] + next
if dp[i] > result {
result = dp[i]
}
}
return result
}