Skip to content

Commit 7fb0b03

Browse files
authored
feat: add solutions to lc problem: No.3147 (#4775)
1 parent 8585d28 commit 7fb0b03

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ tags:
9797

9898
<!-- solution:start -->
9999

100-
### 方法一:枚举 + 后缀和
100+
### 方法一:枚举 + 逆序遍历
101101

102102
我们可以在 $[n - k, n)$ 的范围内枚举终点,然后从终点开始向前遍历,每次累加间隔为 $k$ 的魔法师的能量值,更新答案。
103103

104-
时间复杂度 $O(n)$,其中 $n$ 是数组 `energy` 的长度。空间复杂度 $O(1)$。
104+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{energy}$ 的长度。空间复杂度 $O(1)$。
105105

106106
<!-- tabs:start -->
107107

@@ -190,6 +190,27 @@ function maximumEnergy(energy: number[], k: number): number {
190190
}
191191
```
192192

193+
#### Rust
194+
195+
```rust
196+
impl Solution {
197+
pub fn maximum_energy(energy: Vec<i32>, k: i32) -> i32 {
198+
let n = energy.len();
199+
let mut ans = i32::MIN;
200+
for i in n - k as usize..n {
201+
let mut s = 0;
202+
let mut j = i as i32;
203+
while j >= 0 {
204+
s += energy[j as usize];
205+
ans = ans.max(s);
206+
j -= k;
207+
}
208+
}
209+
ans
210+
}
211+
}
212+
```
213+
193214
<!-- tabs:end -->
194215

195216
<!-- solution:end -->

solution/3100-3199/3147.Taking Maximum Energy From the Mystic Dungeon/README_EN.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ tags:
9898

9999
<!-- solution:start -->
100100

101-
### Solution 1: Enumeration + Suffix Sum
101+
### Solution 1: Enumeration + Reverse Traversal
102102

103-
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.
103+
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.
104104

105-
The time complexity is $O(n)$, where $n$ is the length of the array `energy`. The space complexity is $O(1)$.
105+
The time complexity is $O(n)$, where $n$ is the length of array $\textit{energy}$. The space complexity is $O(1)$.
106106

107107
<!-- tabs:start -->
108108

@@ -191,6 +191,27 @@ function maximumEnergy(energy: number[], k: number): number {
191191
}
192192
```
193193

194+
#### Rust
195+
196+
```rust
197+
impl Solution {
198+
pub fn maximum_energy(energy: Vec<i32>, k: i32) -> i32 {
199+
let n = energy.len();
200+
let mut ans = i32::MIN;
201+
for i in n - k as usize..n {
202+
let mut s = 0;
203+
let mut j = i as i32;
204+
while j >= 0 {
205+
s += energy[j as usize];
206+
ans = ans.max(s);
207+
j -= k;
208+
}
209+
}
210+
ans
211+
}
212+
}
213+
```
214+
194215
<!-- tabs:end -->
195216

196217
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn maximum_energy(energy: Vec<i32>, k: i32) -> i32 {
3+
let n = energy.len();
4+
let mut ans = i32::MIN;
5+
for i in n - k as usize..n {
6+
let mut s = 0;
7+
let mut j = i as i32;
8+
while j >= 0 {
9+
s += energy[j as usize];
10+
ans = ans.max(s);
11+
j -= k;
12+
}
13+
}
14+
ans
15+
}
16+
}

0 commit comments

Comments
 (0)