力扣hot100 跳跃游戏 贪心

Problem: 55. 跳跃游戏
力扣hot100 跳跃游戏 贪心_第1张图片

文章目录

  • 思路
  • 复杂度
  • Code

思路

‍ 参考

  • 挨着跳,记录最远能到达的地方

复杂度

时间复杂度: O ( n ) O(n) O(n)

空间复杂度: O ( 1 ) O(1) O(1)

Code

class Solution {
	public boolean canJump(int[] nums)
	{
		int maxAchieveable = 0;
		for (int i = 0; i < nums.length; i++)
		{
			if (i > maxAchieveable)
				return false;
			maxAchieveable = Math.max(maxAchieveable, i + nums[i]);
		}
		return true;
	}
}

你可能感兴趣的:(力扣,hot100,leetcode,游戏,算法)