Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0045
Browse files Browse the repository at this point in the history
No.0045.Jump Game II
  • Loading branch information
yanglbme committed Jan 27, 2025
1 parent 8974512 commit 8d840f6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 98 deletions.
62 changes: 30 additions & 32 deletions solution/0000-0099/0045.Jump Game II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,17 @@ function jump(nums: number[]): number {
```rust
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut dp = vec![i32::MAX; n];
dp[0] = 0;
for i in 0..n - 1 {
for j in 1..=nums[i] as usize {
if i + j >= n {
break;
}
dp[i + j] = dp[i + j].min(dp[i] + 1);
let mut ans = 0;
let mut mx = 0;
let mut last = 0;
for i in 0..(nums.len() - 1) {
mx = mx.max(i as i32 + nums[i]);
if last == i as i32 {
ans += 1;
last = mx;
}
}
dp[n - 1]
ans
}
}
```
Expand All @@ -206,19 +205,18 @@ public class Solution {
#### C

```c
#define min(a, b) a < b ? a : b
int jump(int* nums, int numsSize) {
int dp[numsSize];
for (int i = 0; i < numsSize; i++) {
dp[i] = numsSize;
}
dp[0] = 0;
for (int i = 0; i < numsSize - 1; i++) {
for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
dp[j] = min(dp[j], dp[i] + 1);
int ans = 0;
int mx = 0;
int last = 0;
for (int i = 0; i < numsSize - 1; ++i) {
mx = (mx > i + nums[i]) ? mx : (i + nums[i]);
if (last == i) {
++ans;
last = mx;
}
}
return dp[numsSize - 1];
return ans;
}
```
Expand All @@ -227,23 +225,23 @@ int jump(int* nums, int numsSize) {
```php
class Solution {
/**
* @param integer[] $nums
* @return integer
* @param Integer[] $nums
* @return Integer
*/
function jump($nums) {
$maxReach = 0;
$steps = 0;
$lastJump = 0;
for ($i = 0; $i <= count($nums) - 2; $i++) {
$maxReach = max($maxReach, $i + $nums[$i]);
if ($i == $lastJump) {
$lastJump = $maxReach;
$steps++;
$ans = 0;
$mx = 0;
$last = 0;
for ($i = 0; $i < count($nums) - 1; $i++) {
$mx = max($mx, $i + $nums[$i]);
if ($last == $i) {
$ans++;
$last = $mx;
}
}
return $steps;
return $ans;
}
}
```
Expand Down
62 changes: 30 additions & 32 deletions solution/0000-0099/0045.Jump Game II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,17 @@ function jump(nums: number[]): number {
```rust
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut dp = vec![i32::MAX; n];
dp[0] = 0;
for i in 0..n - 1 {
for j in 1..=nums[i] as usize {
if i + j >= n {
break;
}
dp[i + j] = dp[i + j].min(dp[i] + 1);
let mut ans = 0;
let mut mx = 0;
let mut last = 0;
for i in 0..(nums.len() - 1) {
mx = mx.max(i as i32 + nums[i]);
if last == i as i32 {
ans += 1;
last = mx;
}
}
dp[n - 1]
ans
}
}
```
Expand All @@ -203,19 +202,18 @@ public class Solution {
#### C

```c
#define min(a, b) a < b ? a : b
int jump(int* nums, int numsSize) {
int dp[numsSize];
for (int i = 0; i < numsSize; i++) {
dp[i] = numsSize;
}
dp[0] = 0;
for (int i = 0; i < numsSize - 1; i++) {
for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
dp[j] = min(dp[j], dp[i] + 1);
int ans = 0;
int mx = 0;
int last = 0;
for (int i = 0; i < numsSize - 1; ++i) {
mx = (mx > i + nums[i]) ? mx : (i + nums[i]);
if (last == i) {
++ans;
last = mx;
}
}
return dp[numsSize - 1];
return ans;
}
```
Expand All @@ -224,23 +222,23 @@ int jump(int* nums, int numsSize) {
```php
class Solution {
/**
* @param integer[] $nums
* @return integer
* @param Integer[] $nums
* @return Integer
*/
function jump($nums) {
$maxReach = 0;
$steps = 0;
$lastJump = 0;
for ($i = 0; $i <= count($nums) - 2; $i++) {
$maxReach = max($maxReach, $i + $nums[$i]);
if ($i == $lastJump) {
$lastJump = $maxReach;
$steps++;
$ans = 0;
$mx = 0;
$last = 0;
for ($i = 0; $i < count($nums) - 1; $i++) {
$mx = max($mx, $i + $nums[$i]);
if ($last == $i) {
$ans++;
$last = $mx;
}
}
return $steps;
return $ans;
}
}
```
Expand Down
21 changes: 10 additions & 11 deletions solution/0000-0099/0045.Jump Game II/Solution.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#define min(a, b) a < b ? a : b
int jump(int* nums, int numsSize) {
int dp[numsSize];
for (int i = 0; i < numsSize; i++) {
dp[i] = numsSize;
}
dp[0] = 0;
for (int i = 0; i < numsSize - 1; i++) {
for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
dp[j] = min(dp[j], dp[i] + 1);
int ans = 0;
int mx = 0;
int last = 0;
for (int i = 0; i < numsSize - 1; ++i) {
mx = (mx > i + nums[i]) ? mx : (i + nums[i]);
if (last == i) {
++ans;
last = mx;
}
}
return dp[numsSize - 1];
}
return ans;
}
25 changes: 12 additions & 13 deletions solution/0000-0099/0045.Jump Game II/Solution.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php
class Solution {
/**
* @param integer[] $nums
* @return integer
* @param Integer[] $nums
* @return Integer
*/

function jump($nums) {
$maxReach = 0;
$steps = 0;
$lastJump = 0;
for ($i = 0; $i <= count($nums) - 2; $i++) {
$maxReach = max($maxReach, $i + $nums[$i]);
if ($i == $lastJump) {
$lastJump = $maxReach;
$steps++;
$ans = 0;
$mx = 0;
$last = 0;

for ($i = 0; $i < count($nums) - 1; $i++) {
$mx = max($mx, $i + $nums[$i]);
if ($last == $i) {
$ans++;
$last = $mx;
}
}

return $steps;
return $ans;
}
}
19 changes: 9 additions & 10 deletions solution/0000-0099/0045.Jump Game II/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut dp = vec![i32::MAX; n];
dp[0] = 0;
for i in 0..n - 1 {
for j in 1..=nums[i] as usize {
if i + j >= n {
break;
}
dp[i + j] = dp[i + j].min(dp[i] + 1);
let mut ans = 0;
let mut mx = 0;
let mut last = 0;
for i in 0..(nums.len() - 1) {
mx = mx.max(i as i32 + nums[i]);
if last == i as i32 {
ans += 1;
last = mx;
}
}
dp[n - 1]
ans
}
}

0 comments on commit 8d840f6

Please sign in to comment.