leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)

45. Jump Game II

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.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is2. (Jump1 step from index 0 to 1, then3 steps to the last index.)

Note:
You can assume that you can always reach the last index.

给定一个非负整数数组,给定的初始化位置在数组的起始位置。数组中的每个元素代表着你能都在此位置跳跃的最大的距离。你的目标是用最少的跳跃数达到数组的末尾。算法:贪心

第一种方法:(贪心)

public int jump(int[] nums) {
		if (nums.length <= 1)
			return 0;
		if (nums[0] == 0)
			return -1;
		// 记录当前活动距离
		int reach = nums[0];
		int steps = 0, start = 0;
		for (; start < nums.length && start <= reach;) {
			++steps;
			if (reach >= nums.length - 1) {
				return steps;
			}
			// nextMax表示下一步能到达的最远距离
			int nextMax = 0;
			// 在当前start和reach之间,找下一步能到达最远的距离的下标
			for (int i = start; i <= reach; ++i) {
				if ((i + nums[i]) > nextMax) {
					nextMax = i + nums[i];
					start = i;
				}
			}
			reach = nextMax;
		}
		return -1;
	}

第二种方法:(贪心)

// Time Complexity: O(n). Space: O(1).
	public int jump(int[] nums) {
		if (nums == null || nums.length == 0) {
			return -1;
		}
		// cur是维护的当前能跳到的最大位置
		// 第step+1步,能到达的最远距离
		int cur = 0;
		// last是指从之前的点能reach到得最远位置
		// 已经可以到达的最大距离
		int last = 0;
		int step = 0;
		for (int i = 0; i < nums.length/* && i <= cur */; i++) {
			// 当i 大于之前点能碰到的最大位置时,就需要跳一步,
			// 并把last更新为cur.
			if (i > last) {
				step++;
				last = cur;
			}
			// 计算step+1的最大距离
			cur = Math.max(cur, nums[i] + i);
		}
		// 最后返回若是cur能到最后一个元素,就返回step,
		// 若是到不了,就说明根本走不到最后一步,返回-1.
		return cur < nums.length - 1 ? -1 : step;
	}

55. 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.

每次跳跃选择往最远处跳跃,如果最后能够跳到数组最后一位或者最后一位之后,那么一定存在一种跳跃方式正好跳到最后一位上,

第一种方法(动态规划)

public boolean canJump(int[] nums) {
		int n = nums.length;
		// dp[i]表示当前跳跃的最大距离
		int dp[] = new int[n];
		dp[0] = nums[0];
		// i表示当前距离,也是下标
		for (int i = 1; i < n; i++) {
			// i点可达
			if (i <= dp[i - 1])
				dp[i] = Math.max(dp[i - 1], i + nums[i]);
			else
				dp[i] = dp[i - 1];
		}
		return dp[n - 1] >= (n - 1);
	}

第二种方法(贪心算法)

 维护一个当前能跳到的最大值maxJump, 若是maxJump 已经>=nums.length-1, 说明能跳到最后一个点,return true.若是过程中maxJump <= i, 说明跳到当前点便不能往前,跳出loop, return false.

public boolean canJump(int[] nums) {
		int n = nums.length;
		// maxJump是维护的当前能跳到的最大位置
		int maxJump = 0;
		// for (int i = 0; i < n && i <= maxJump; ++i)
		// maxJump = Math.max(nums[i] + i, maxJump);
		for (int i = 0; i < n; i++) {
			// i>maxJump表示无法到达i的位置,失败
			// maxJump >= (n - 1),此时的距离已经足够到达终点,成功
			if (i > maxJump || maxJump >= (n - 1))
				break;
			// nums[i]+i当前跳最远距离 maxJump为i之前跳最远距离
			maxJump = Math.max(maxJump, i + nums[i]);
		}
		return maxJump >= (n - 1);
	}



你可能感兴趣的:(leetcode)