Skip to content

Commit 8424a43

Browse files
committed
Add solution and test-cases for problem 3147
1 parent f03226b commit 8424a43

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [3147.Taking Maximum Energy From the Mystic Dungeon][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
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+
6+
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.
7+
8+
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.
9+
10+
You are given an array `energy` and an integer `k`. Return the **maximum** possible energy you can gain.
11+
12+
**Note** that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
17+
Input: energy = [5,2,-10,-5,1], k = 3
18+
19+
Output: 3
1420
15-
## 题意
16-
> ...
21+
Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
22+
```
1723

18-
## 题解
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Taking Maximum Energy From the Mystic Dungeon
23-
```go
2426
```
27+
Input: energy = [-2,-3,-1], k = 2
2528
29+
Output: -1
30+
31+
Explanation: We can gain a total energy of -1 by starting from magician 2.
32+
```
2633

2734
## 结语
2835

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(energy []int, k int) int {
4+
dp := make([]int, len(energy))
5+
// k = 2
6+
// 1, 2, 3, 4,
7+
l := len(energy)
8+
ret := energy[l-1]
9+
for i := l - 1; i >= l-k; i-- {
10+
// 这些是无法跳的,所以默认就有自己的值
11+
dp[i] = energy[i]
12+
ret = max(ret, dp[i])
13+
}
14+
for i := l - k - 1; i >= 0; i-- {
15+
dp[i] = dp[i+k] + energy[i]
16+
ret = max(ret, dp[i])
17+
}
18+
return ret
519
}

leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{5, 2, -10, -5, 1}, 3, 3},
18+
{"TestCase2", []int{-2, -3, -1}, 2, -1},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)