-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update solutions to lc problem: No.0045
No.0045.Jump Game II
- Loading branch information
Showing
5 changed files
with
91 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |