leetcode 55. Jump Game & 45. Jump Game II

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

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
             jump length is 0, which makes it impossible to reach the last index.

二 分析

 medium级别,题目给定非负数的数组,每个元素表示你可以走的最大步数。问能否到达最后的元素。

分析: 定义max, 为当前元素能到达的最远距离,根据题目意思,max =索引位置+跳跃距离。即 max= i+nums[i].

能到达的条件:max>=nums.length-1;

 到达不了,就如例2 的case:max

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] nums ={3,2,1,0,4 };
		System.out.println(canJump(nums));
		int[] nums1 ={0,2,3};
		System.out.println(canJump(nums1));
		int[] nums2 ={3,2,1,1,4 };
		System.out.println(canJump(nums2));
	}

	//贪心
	public static boolean canJump(int[] nums) {
		//coner case
		if(nums.length==1 ){
			return true;
		}

		int max = 0;
		
		for(int i=0;i< nums.length;i++){
			//到不了
			if(max< i){
				return false;
			}//能到最后
			if(max>= nums.length-1){
				return true;
			}//计算当前能跳到的最远位置
			max = Math.max(max, i+nums[i]);
			
		}
		
		return false;        
    }

Runtime: 1 ms, faster than 99.11% of Java online submissions for Jump Game.

Memory Usage: 39.3 MB, less than 100.00% of Java online submissions for Jump Game.

时间复杂度:O(N)

45 jump ganme 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.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
    Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:

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

分析

hard 级别,跟上个题目类似,求到达最后一个位置的最少跳跃数,默认是一定能到达最后的。

还是贪心算法,但是这里的贪心不是本次最大(不一定是最优解),而是本次最大范围内,下一跳距离能到最大(索引位置+ 下一跳距离),一旦当这个范围到达末尾时,当前所用的步数一定是最小步数。这个题我没做出来,是看了算法珠玑。https://soulmachine.gitbooks.io/algorithm-essentials/cpp/greedy/jump-game-ii.html

   我们使用两个变量cur和pre分别来保存当前的能到达的最远位置和之前能到达的最远位置,当前最远位置超过上一跳pre的范围时(意味着需要再跳一步了),去更新步数,把pre更新为cur.

 代码如下:

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] nums ={2,3,1,1,4};
		System.out.println( jump( nums) );
	}
	
	//贪心算法
	 public static int jump(int[] nums) {
	     int res =0;
		 //coner case 
		 if(nums.length==1){
			 return res;
		 }
		 //当前最大范围
		 int cur =0;
		 //上一次最大范围
		 int pre =0;
		 //rule
		for(int i=0;ipre){
				 res ++;
				 pre =cur;//更新上一跳
			 }
			 //当前位置加跳跃距离更新cur
			 cur = Math.max(cur , i+nums[i]);
			 
		 }
		 
		 return res;
	 }

Runtime: 1 ms, faster than 99.99% of Java online submissions for Jump Game II.

Memory Usage: 38.2 MB, less than 100.00% of Java online submissions for Jump Game II.

时间 复杂度:O(N)

你可能感兴趣的:(leetcode,jump,贪心算法,LeetCode,数组)