Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc41ac67

Browse files
committed
Add solution #3147
1 parent6936555 commitc41ac67

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,7 @@
26602660
3135|[Equalize Strings by Adding or Removing Characters at Ends](./solutions/3135-equalize-strings-by-adding-or-removing-characters-at-ends.js)|Medium|
26612661
3136|[Valid Word](./solutions/3136-valid-word.js)|Easy|
26622662
3141|[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|
26632664
3151|[Special Array I](./solutions/3151-special-array-i.js)|Easy|
26642665
3155|[Maximum Number of Upgradable Servers](./solutions/3155-maximum-number-of-upgradable-servers.js)|Medium|
26652666
3157|[Find the Level of Tree with Minimum Sum](./solutions/3157-find-the-level-of-tree-with-minimum-sum.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
varmaximumEnergy=function(energy,k){
29+
constn=energy.length;
30+
constdp=newArray(n).fill(0);
31+
letresult=-Infinity;
32+
33+
for(leti=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+
returnresult;
39+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp