LeetCode Jump Game

LeetCode Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.



class Solution {
	bool canJump(vector<int>& nums) {
		int maxpos = 0;
		for (int i = 0; i < nums.size(); ++i) {
			if (i > maxpos)break;
			maxpos = max(maxpos, i + nums[i]);
		return maxpos >= nums.size() - 1;


One thought on “LeetCode Jump Game

  1. Pingback: LeetCode Jump Game II | bitJoy > code

Leave a Reply

Your email address will not be published. Required fields are marked *