From 6253f3c883496d81d0a87d92cf21da8b894701e4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 10 Oct 2025 07:01:03 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3147 --- .../README.md | 25 +++++++++++++++-- .../README_EN.md | 27 ++++++++++++++++--- .../Solution.rs | 16 +++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/Solution.rs diff --git a/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README.md b/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README.md index 5c21898309773..01b4a3fa27625 100644 --- a/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README.md +++ b/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README.md @@ -97,11 +97,11 @@ tags: -### 方法一:枚举 + 后缀和 +### 方法一:枚举 + 逆序遍历 我们可以在 $[n - k, n)$ 的范围内枚举终点,然后从终点开始向前遍历,每次累加间隔为 $k$ 的魔法师的能量值,更新答案。 -时间复杂度 $O(n)$,其中 $n$ 是数组 `energy` 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{energy}$ 的长度。空间复杂度 $O(1)$。 @@ -190,6 +190,27 @@ function maximumEnergy(energy: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_energy(energy: Vec, k: i32) -> i32 { + let n = energy.len(); + let mut ans = i32::MIN; + for i in n - k as usize..n { + let mut s = 0; + let mut j = i as i32; + while j >= 0 { + s += energy[j as usize]; + ans = ans.max(s); + j -= k; + } + } + ans + } +} +``` + diff --git a/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README_EN.md b/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README_EN.md index d6a4d80e48d1c..b5e401697dcb4 100644 --- a/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README_EN.md +++ b/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README_EN.md @@ -98,11 +98,11 @@ tags: -### Solution 1: Enumeration + Suffix Sum +### Solution 1: Enumeration + Reverse Traversal -We can enumerate the endpoint in the range of $[n - k, n)$, then start from the endpoint and traverse backwards, adding the energy value of the magician at each $k$ interval, and update the answer. +We can enumerate the endpoints within the range $[n - k, n)$, then traverse backwards from each endpoint, accumulating the energy values of wizards at intervals of $k$, and update the answer. -The time complexity is $O(n)$, where $n$ is the length of the array `energy`. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of array $\textit{energy}$. The space complexity is $O(1)$. @@ -191,6 +191,27 @@ function maximumEnergy(energy: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_energy(energy: Vec, k: i32) -> i32 { + let n = energy.len(); + let mut ans = i32::MIN; + for i in n - k as usize..n { + let mut s = 0; + let mut j = i as i32; + while j >= 0 { + s += energy[j as usize]; + ans = ans.max(s); + j -= k; + } + } + ans + } +} +``` + diff --git a/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/Solution.rs b/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/Solution.rs new file mode 100644 index 0000000000000..cf501fd118328 --- /dev/null +++ b/solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn maximum_energy(energy: Vec, k: i32) -> i32 { + let n = energy.len(); + let mut ans = i32::MIN; + for i in n - k as usize..n { + let mut s = 0; + let mut j = i as i32; + while j >= 0 { + s += energy[j as usize]; + ans = ans.max(s); + j -= k; + } + } + ans + } +}