File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 266026603135|[ Equalize Strings by Adding or Removing Characters at Ends] ( ./solutions/3135-equalize-strings-by-adding-or-removing-characters-at-ends.js ) |Medium|
266126613136|[ Valid Word] ( ./solutions/3136-valid-word.js ) |Easy|
266226623141|[ Maximum Hamming Distances] ( ./solutions/3141-maximum-hamming-distances.js ) |Hard|
2663+ 3147|[ Taking Maximum Energy From the Mystic Dungeon] ( ./solutions/3147-taking-maximum-energy-from-the-mystic-dungeon.js ) |Medium|
266326643151|[ Special Array I] ( ./solutions/3151-special-array-i.js ) |Easy|
266426653155|[ Maximum Number of Upgradable Servers] ( ./solutions/3155-maximum-number-of-upgradable-servers.js ) |Medium|
266526663157|[ Find the Level of Tree with Minimum Sum] ( ./solutions/3157-find-the-level-of-tree-with-minimum-sum.js ) |Medium|
Original file line number Diff line number Diff line change 1+ /**
2+ * 3147. Taking Maximum Energy From the Mystic Dungeon
3+ * https://leetcode.com/problems/taking-maximum-energy-from-the-mystic-dungeon/
4+ * Difficulty: Medium
5+ *
6+ * In a mystic dungeon, n magicians are standing in a line. Each magician has an attribute
7+ * that gives you energy. Some magicians can give you negative energy, which means taking
8+ * energy from you.
9+ *
10+ * You have been cursed in such a way that after absorbing energy from magician i, you will
11+ * be instantly transported to magician (i + k). This process will be repeated until you
12+ * reach the magician where (i + k) does not exist.
13+ *
14+ * In other words, you will choose a starting point and then teleport with k jumps until you
15+ * reach the end of the magicians' sequence, absorbing all the energy during the journey.
16+ *
17+ * You are given an array energy and an integer k. Return the maximum possible energy you can gain.
18+ *
19+ * Note that when you are reach a magician, you must take energy from them, whether it is negative
20+ * or positive energy.
21+ */
22+
23+ /**
24+ * @param {number[] } energy
25+ * @param {number } k
26+ * @return {number }
27+ */
28+ var maximumEnergy = function ( energy , k ) {
29+ const n = energy . length ;
30+ const dp = new Array ( n ) . fill ( 0 ) ;
31+ let result = - Infinity ;
32+
33+ for ( let i = n - 1 ; i >= 0 ; i -- ) {
34+ dp [ i ] = energy [ i ] + ( i + k < n ? dp [ i + k ] : 0 ) ;
35+ result = Math . max ( result , dp [ i ] ) ;
36+ }
37+
38+ return result ;
39+ } ;
You can’t perform that action at this time.
0 commit comments