-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump_game.php
68 lines (55 loc) · 1.84 KB
/
jump_game.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Jump Game
* @link https://leetcode.com/problems/jump-game
*
* Approach:
* 1. Take each element where we can jump from current position and find optimal one,
* in this case optimal one is the one that can jump further than others.
* 2. After this, jump to this optimal position and then repeat 1st step,
* note that for the algorithm work linearly we start checking each element where we can jump,
* from element that stays after the last element where we could jump from previous position.
* (All elements that we access early and that stays after the optimal one are not require to check,
* because they are stay after optimal one and still can't jump further)
*/
class Solution {
public function canJump($nums)
{
$start = 1;
$end = $nums[0];
for ($i = 0; $i < count($nums) - 1; $i++) {
if ($nums[$i] === 0) {
return false;
}
if ($end >= count($nums) - 1) {
return true;
}
$optimalValue = 0;
$optimalIdx = 0;
while ($start <= $end) {
if ($nums[$start] + $start >= $optimalValue) {
$optimalIdx = $start;
$optimalValue = $nums[$start] + $start;
}
$start++;
}
$end = $optimalValue;
$i = $optimalIdx - 1;
}
return true;
}
/**
* Keep track of max index where we can jump,
* if current index is greater than max index, then we can't access the top.
*/
public function canJumpReadable($nums) {
$maxJump = 0;
for ($i = 0; $i < count($nums); $i++) {
if ($maxJump < $i) {
return false;
}
$maxJump = max($maxJump, ($i + $nums[$i]));
}
return true;
}
}