LeetCode 55: Jump Game Solution
Master LeetCode problem 55 (Jump Game), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
55. Jump Game
Problem Explanation
Explanation
To solve this problem, we can use a greedy approach. We iterate through the array from left to right, keeping track of the furthest index we can reach. At each index, we update the furthest index we can reach based on the current element's value and the furthest index we have seen so far. If at any point, the current index is greater than the furthest index we can reach, we return false as it means we cannot reach the end. If we successfully reach the end of the array, we return true.
- Time complexity: O(n), where n is the number of elements in the input array.
- Space complexity: O(1)
Solution Code
class Solution {
public boolean canJump(int[] nums) {
int maxReach = 0;
for (int i = 0; i < nums.length; i++) {
if (i > maxReach) {
return false;
}
maxReach = Math.max(maxReach, i + nums[i]);
if (maxReach >= nums.length - 1) {
return true;
}
}
return false;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 55 (Jump Game)?
This page provides optimized solutions for LeetCode problem 55 (Jump Game) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 55 (Jump Game)?
The time complexity for LeetCode 55 (Jump Game) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 55 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 55 in Java, C++, or Python.