Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [3147.Taking Maximum Energy From the Mystic Dungeon][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

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.

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.

You are given an array `energy` and an integer `k`. Return the **maximum** possible energy you can gain.

**Note** that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: energy = [5,2,-10,-5,1], k = 3

Output: 3

## 题意
> ...
Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
```

## 题解
**Example 2:**

### 思路1
> ...
Taking Maximum Energy From the Mystic Dungeon
```go
```
Input: energy = [-2,-3,-1], k = 2

Output: -1

Explanation: We can gain a total energy of -1 by starting from magician 2.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(energy []int, k int) int {
dp := make([]int, len(energy))
// k = 2
// 1, 2, 3, 4,
l := len(energy)
ret := energy[l-1]
for i := l - 1; i >= l-k; i-- {
// 这些是无法跳的,所以默认就有自己的值
dp[i] = energy[i]
ret = max(ret, dp[i])
}
for i := l - k - 1; i >= 0; i-- {
dp[i] = dp[i+k] + energy[i]
ret = max(ret, dp[i])
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{5, 2, -10, -5, 1}, 3, 3},
{"TestCase2", []int{-2, -3, -1}, 2, -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading