Description
https://leetcode.com/problems/jump-game/description/
Brutal Solution
Traverse from the start node, mark its range as true, then we can check the end of the list when traversion finished.
1 | class Solution { |
Mark-Free Solution
In the solution above, we found some redundant traversion in the nested loop, to improve it, we should skip the meaningless procedure. To clarify how it works, we should introduce a pre-rule discovered by the fact:
If the item A is reachable, then the item before A must be reachable.
Then we use a varialbe rightIndex
to record the reachable boundary in the right.
1 | class Solution { |
Trimmed Solution
In the solution above, the procedure need to traverse all node regardless of the dataset. In fact we can terminate the result once we reached the end item of liste(success), or the current index is bigger than the rightIndex
(fail)
1 | class Solution { |