diff --git a/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/README.md b/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/README.md index 31a002b33..8711aea7b 100755 --- a/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/README.md +++ b/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution.go b/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution.go index d115ccf5e..554de5b30 100644 --- a/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution.go +++ b/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution.go @@ -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 } diff --git a/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution_test.go b/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution_test.go index 14ff50eb4..ad856c1b7 100644 --- a/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution_test.go +++ b/leetcode/3101-3200/3147.Taking-Maximum-Energy-From-the-Mystic-Dungeon/Solution_test.go @@ -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() { }