java: add «55. Jump Game»
URL: https://leetcode.com/problems/jump-game/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
eb0e5a5391
commit
601d5a1f68
1 changed files with 15 additions and 0 deletions
15
java/jump-game.java
Normal file
15
java/jump-game.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
class Solution {
|
||||
public boolean canJump(int[] nums) {
|
||||
var last = 0;
|
||||
for (var i = 0; i < nums.length && last < nums.length - 1; ++i) {
|
||||
if (i > last) {
|
||||
// can't reach the current position
|
||||
return false;
|
||||
}
|
||||
|
||||
last = Math.max(last, i + nums[i]);
|
||||
}
|
||||
|
||||
return last >= nums.length - 1;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue