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:
Matej Focko 2025-01-04 17:31:27 +01:00
parent eb0e5a5391
commit 601d5a1f68
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

15
java/jump-game.java Normal file
View 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;
}
}