3147. Taking Maximum Energy From the Mystic Dungeon Medium

@problem@discussion
#Array#Prefix Sum



1/**
2 * [3147] Taking Maximum Energy From the Mystic Dungeon
3 *
4 * 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.
5 * 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.
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 * You are given an array energy and an integer k. Return the maximum possible energy you can gain.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block" style="
11 *     border-color: var(--border-tertiary);
12 *     border-left-width: 2px;
13 *     color: var(--text-secondary);
14 *     font-size: .875rem;
15 *     margin-bottom: 1rem;
16 *     margin-top: 1rem;
17 *     overflow: visible;
18 *     padding-left: 1rem;
19 * ">
20 * Input: <span class="example-io" style="
21 *     font-family: Menlo,sans-serif;
22 *     font-size: 0.85rem;
23 * "> energy = [5,2,-10,-5,1], k = 3</span>
24 * Output:<span class="example-io" style="
25 *     font-family: Menlo,sans-serif;
26 *     font-size: 0.85rem;
27 * "> 3</span>
28 * Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
29 * </div>
30 * <strong class="example">Example 2:
31 * <div class="example-block" style="
32 *     border-color: var(--border-tertiary);
33 *     border-left-width: 2px;
34 *     color: var(--text-secondary);
35 *     font-size: .875rem;
36 *     margin-bottom: 1rem;
37 *     margin-top: 1rem;
38 *     overflow: visible;
39 *     padding-left: 1rem;
40 * ">
41 * Input:<span class="example-io" style="
42 *     font-family: Menlo,sans-serif;
43 *     font-size: 0.85rem;
44 * "> energy = [-2,-3,-1], k = 2</span>
45 * Output:<span class="example-io" style="
46 *     font-family: Menlo,sans-serif;
47 *     font-size: 0.85rem;
48 * "> -1</span>
49 * Explanation: We can gain a total energy of -1 by starting from magician 2.
50 * </div>
51 *  
52 * Constraints:
53 * 
54 * 	1 <= energy.length <= 10^5
55 * 	-1000 <= energy[i] <= 1000
56 * 	1 <= k <= energy.length - 1
57 * 
58 *  
59 * ​​​​​​
60 */
61pub struct Solution {}
62
63// problem: https://leetcode.com/problems/taking-maximum-energy-from-the-mystic-dungeon/
64// discuss: https://leetcode.com/problems/taking-maximum-energy-from-the-mystic-dungeon/discuss/?currentPage=1&orderBy=most_votes&query=
65
66// submission codes start here
67
68impl Solution {
69    pub fn maximum_energy(energy: Vec<i32>, k: i32) -> i32 {
70        0
71    }
72}
73
74// submission codes end
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_3147() {
82    }
83}
84


Back
© 2025 bowen.ge All Rights Reserved.