Skip to content

Commit c4984df

Browse files
authored
feat: add rust solution for lc No.2770 (#5190)
1 parent 880a7bb commit c4984df

4 files changed

Lines changed: 100 additions & 15 deletions

File tree

solution/2700-2799/2770.Maximum Number of Jumps to Reach the Last Index/README.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ tags:
3939
<pre><strong>输入:</strong>nums = [1,3,6,4,1,2], target = 2
4040
<strong>输出:</strong>3
4141
<strong>解释:</strong>要想以最大跳跃次数从下标 0 到下标 n - 1 ,可以按下述跳跃序列执行操作:
42-
- 从下标 0 跳跃到下标 1 。
43-
- 从下标 1 跳跃到下标 3 。
44-
- 从下标 3 跳跃到下标 5 。
42+
- 从下标 0 跳跃到下标 1 。
43+
- 从下标 1 跳跃到下标 3 。
44+
- 从下标 3 跳跃到下标 5 。
4545
可以证明,从 0 到 n - 1 的所有方案中,不存在比 3 步更长的跳跃序列。因此,答案是 3 。 </pre>
4646

4747
<p><strong>示例 2:</strong></p>
4848

4949
<pre><strong>输入:</strong>nums = [1,3,6,4,1,2], target = 3
5050
<strong>输出:</strong>5
5151
<strong>解释:</strong>要想以最大跳跃次数从下标 0 到下标 n - 1 ,可以按下述跳跃序列执行操作:
52-
- 从下标 0 跳跃到下标 1 。
53-
- 从下标 1 跳跃到下标 2 。
54-
- 从下标 2 跳跃到下标 3 。
55-
- 从下标 3 跳跃到下标 4 。
56-
- 从下标 4 跳跃到下标 5 。
52+
- 从下标 0 跳跃到下标 1 。
53+
- 从下标 1 跳跃到下标 2 。
54+
- 从下标 2 跳跃到下标 3 。
55+
- 从下标 3 跳跃到下标 4 。
56+
- 从下标 4 跳跃到下标 5 。
5757
可以证明,从 0 到 n - 1 的所有方案中,不存在比 5 步更长的跳跃序列。因此,答案是 5 。 </pre>
5858

5959
<p><strong>示例 3:</strong></p>
6060

6161
<pre><strong>输入:</strong>nums = [1,3,6,4,1,2], target = 0
6262
<strong>输出:</strong>-1
63-
<strong>解释:</strong>可以证明不存在从 0 到 n - 1 的跳跃序列。因此,答案是 -1 。
63+
<strong>解释:</strong>可以证明不存在从 0 到 n - 1 的跳跃序列。因此,答案是 -1 。
6464
</pre>
6565

6666
<p>&nbsp;</p>
@@ -161,7 +161,7 @@ public:
161161
int n = nums.size();
162162
int f[n];
163163
memset(f, -1, sizeof(f));
164-
function<int(int)> dfs = [&](int i) {
164+
auto dfs = [&](this auto&& dfs, int i) -> int {
165165
if (i == n - 1) {
166166
return 0;
167167
}
@@ -248,6 +248,36 @@ function maximumJumps(nums: number[], target: number): number {
248248
}
249249
```
250250

251+
#### Rust
252+
253+
```rust
254+
impl Solution {
255+
pub fn maximum_jumps(nums: Vec<i32>, target: i32) -> i32 {
256+
let n = nums.len();
257+
let mut f = vec![-1; n];
258+
259+
fn dfs(i: usize, nums: &Vec<i32>, target: i32, f: &mut Vec<i32>) -> i32 {
260+
if i == nums.len() - 1 {
261+
return 0;
262+
}
263+
if f[i] != -1 {
264+
return f[i];
265+
}
266+
f[i] = -(1 << 30);
267+
for j in i + 1..nums.len() {
268+
if (nums[i] - nums[j]).abs() <= target {
269+
f[i] = f[i].max(1 + dfs(j, nums, target, f));
270+
}
271+
}
272+
f[i]
273+
}
274+
275+
let ans = dfs(0, &nums, target, &mut f);
276+
if ans < 0 { -1 } else { ans }
277+
}
278+
}
279+
```
280+
251281
<!-- tabs:end -->
252282

253283
<!-- solution:end -->

solution/2700-2799/2770.Maximum Number of Jumps to Reach the Last Index/README_EN.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tags:
3939
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 2
4040
<strong>Output:</strong> 3
4141
<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
42-
- Jump from index 0 to index 1.
42+
- Jump from index 0 to index 1.
4343
- Jump from index 1 to index 3.
4444
- Jump from index 3 to index 5.
4545
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. </pre>
@@ -62,7 +62,7 @@ It can be proven that there is no other jumping sequence that goes from 0 to n -
6262
<pre>
6363
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 0
6464
<strong>Output:</strong> -1
65-
<strong>Explanation:</strong> It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
65+
<strong>Explanation:</strong> It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
6666
</pre>
6767

6868
<p>&nbsp;</p>
@@ -162,7 +162,7 @@ public:
162162
int n = nums.size();
163163
int f[n];
164164
memset(f, -1, sizeof(f));
165-
function<int(int)> dfs = [&](int i) {
165+
auto dfs = [&](this auto&& dfs, int i) -> int {
166166
if (i == n - 1) {
167167
return 0;
168168
}
@@ -249,6 +249,36 @@ function maximumJumps(nums: number[], target: number): number {
249249
}
250250
```
251251

252+
#### Rust
253+
254+
```rust
255+
impl Solution {
256+
pub fn maximum_jumps(nums: Vec<i32>, target: i32) -> i32 {
257+
let n = nums.len();
258+
let mut f = vec![-1; n];
259+
260+
fn dfs(i: usize, nums: &Vec<i32>, target: i32, f: &mut Vec<i32>) -> i32 {
261+
if i == nums.len() - 1 {
262+
return 0;
263+
}
264+
if f[i] != -1 {
265+
return f[i];
266+
}
267+
f[i] = -(1 << 30);
268+
for j in i + 1..nums.len() {
269+
if (nums[i] - nums[j]).abs() <= target {
270+
f[i] = f[i].max(1 + dfs(j, nums, target, f));
271+
}
272+
}
273+
f[i]
274+
}
275+
276+
let ans = dfs(0, &nums, target, &mut f);
277+
if ans < 0 { -1 } else { ans }
278+
}
279+
}
280+
```
281+
252282
<!-- tabs:end -->
253283

254284
<!-- solution:end -->

solution/2700-2799/2770.Maximum Number of Jumps to Reach the Last Index/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Solution {
44
int n = nums.size();
55
int f[n];
66
memset(f, -1, sizeof(f));
7-
function<int(int)> dfs = [&](int i) {
7+
auto dfs = [&](this auto&& dfs, int i) -> int {
88
if (i == n - 1) {
99
return 0;
1010
}
@@ -22,4 +22,4 @@ class Solution {
2222
int ans = dfs(0);
2323
return ans < 0 ? -1 : ans;
2424
}
25-
};
25+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
pub fn maximum_jumps(nums: Vec<i32>, target: i32) -> i32 {
3+
let n = nums.len();
4+
let mut f = vec![-1; n];
5+
6+
fn dfs(i: usize, nums: &Vec<i32>, target: i32, f: &mut Vec<i32>) -> i32 {
7+
if i == nums.len() - 1 {
8+
return 0;
9+
}
10+
if f[i] != -1 {
11+
return f[i];
12+
}
13+
f[i] = -(1 << 30);
14+
for j in i + 1..nums.len() {
15+
if (nums[i] - nums[j]).abs() <= target {
16+
f[i] = f[i].max(1 + dfs(j, nums, target, f));
17+
}
18+
}
19+
f[i]
20+
}
21+
22+
let ans = dfs(0, &nums, target, &mut f);
23+
if ans < 0 { -1 } else { ans }
24+
}
25+
}

0 commit comments

Comments
 (0)