1. 程式人生 > >Leetcode 45 Jump Game II 跳躍遊戲2 最好畫一下圖

Leetcode 45 Jump Game II 跳躍遊戲2 最好畫一下圖

題目:

給定一個非負整數陣列,你最初位於陣列的第一個位置。

陣列中的每個元素代表你在該位置可以跳躍的最大長度。

你的目標是使用最少的跳躍次數到達陣列的最後一個位置。

示例:

輸入: [2,3,1,1,4]
輸出: 2
解釋: 跳到最後一個位置的最小跳躍數是 2。
     從下標為 0 跳到下標為 1 的位置,跳 1 步,然後跳 3 步到達陣列的最後一個位置。

這道題,直接想到的辦法如下:但是執行時間有點兒長。

package test;

public class LC45Try1
{
	public int jump(int[] nums)
	{
		int len = nums.length;
		int[] dp=new int[len];
        for(int i=0;i<len;i++){
        	dp[i]=i;
        }
        for(int i=0;i<len;i++){
        	int t= nums[i];
        	for(int j=1;j<=t;j++){
        		if(i+j==len){
        			break;
        		}
        		if(dp[i+j]>dp[i]+1){
        			dp[i+j]=dp[i]+1;
        		}
        	}
        }
		return dp[len-1];

	}
	public static void main(String[] args)
	{
		LC45Try1 t = new LC45Try1();
		int[] nums = new int[]{2,3,1,1,4};
		System.out.println(t.jump(nums));
	}

}
新增陣列,但執行時間有點長了,於是我就想別的辦法,你可以畫圖,算出跳一次,能走的最遠距離,跳兩部能走的最遠距離,依次。程式碼如下:

package test;

public class LC45Try2
{
	public int jump(int[] nums)
	{
		int len = nums.length;
		
        int max=nums[0];//記錄下次能走的最遠的距離
        int count=0;//走的次數
        int tag=nums[0];//這次能走的最遠距離
        for(int i=1;i<len;i++){
        	if(max>=len-1){
        		return count+1;
        	}
        	for(int j=i;j<=tag;j++){
        		int t=j+nums[j];
            	if(t>max){
            		max=t;
            	}
        	}
        	i=tag;
        	tag=max;
        	count++;
        }
		return count;

	}
	public static void main(String[] args)
	{
		LC45Try2 t = new LC45Try2();
		int[] nums = new int[]{7,0,9,6,9,6,1,7,9,0,1,2,9,0,3};
		System.out.println(t.jump(nums));
	}

}

但我覺得,我寫的太醜了,這是別人寫的美的:

package test;

public class LC45Try3
{
	// 雖然執行時間一樣,但還是覺得人家的簡單
	public int jump(int[] nums)
	{
		if (nums == null || nums.length < 2)
			return 0;
		int jumps = 0;
		int curEnd = 0;
		int curMax = 0;
		for (int i = 0; i < nums.length - 1; i++)
		{
			curMax = Math.max(curMax, nums[i] + i);
			if (i == curEnd)
			{
				jumps++;
				curEnd = curMax;
			}
		}
		return jumps;
	}

}
但也許還是第二種比較容易理解。哈哈

相關推薦

Leetcode 45 Jump Game II 跳躍遊戲2 最好下圖

題目:給定一個非負整數陣列,你最初位於陣列的第一個位置。陣列中的每個元素代表你在該位置可以跳躍的最大長度。你的目標是使用最少的跳躍次數到達陣列的最後一個位置。示例:輸入: [2,3,1,1,4] 輸出: 2 解釋: 跳到最後一個位置的最小跳躍數是 2。   從下標為 0

LeetCode 45. Jump Game II 跳躍遊戲II,求最少跳躍次數 (貪心)

給定一個非負整數陣列,你最初位於陣列的第一個位置。 陣列中的每個元素代表你在該位置可以跳躍的最大長度。 你的目標是使用最少的跳躍次數到達陣列的最後一個位置。 示例: 輸入: [2,3,1,1,4] 輸出: 2 解釋: 跳到最後一個位置的最小跳躍數

[leetcode]45. Jump Game II 跳棋遊戲2 C++/PYTHON實現【hard難度】

題目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in th

leetCode 45.Jump Game II (跳躍遊戲) 解題思路和方法

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 a

19.2.7 [LeetCode 45] Jump Game II

public 很多 號稱 tor ++ 簡單 repr integer hellip Given an array of non-negative integers, you are initially positioned at the first index of th

LeetCode 55. Jump Game跳躍遊戲

mat col lean osi pub 情況 you track rip Given an array of non-negative integers, you are initially positioned at the first index of the ar

leetcode 45. Jump Game II

nbsp 什麽 sent leetcode minimum span ini rst blog Given an array of non-negative integers, you are initially positioned at the first inde

[LeetCode] 45. Jump Game II Java

心算 all can earch rst eps maximum position number 題目: Given an array of non-negative integers, you are initially positioned at the first i

LeetCode-45. Jump Game II

0.原題 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents y

LeetCode 45. Jump Game II (貪心/bfs,dfs超時)

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

python leetcode 45. Jump Game II

cur,last分別表示當前這一步,上一步能到達的最大下標位置 如果當前下標i>last: 步數加1 更新last=cur 否則:更新cur 由於假設能到最後位置,所以省去了判斷是否能到。 class Solution: def jump(sel

leetcode | 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

Leetcode 45. Jump Game II(貪心)

45. Jump Game II 題目連結:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non-negative integers, you are initially positione

LeetCode(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 re

LeetCode 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 a

[LeetCode]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 repres

LeetCode-45-Jump Game II

put minimum col urn assume example code color max 算法描述: Given an array of non-negative integers, you are initially positioned at the firs

**LeetCode 45. Jump Game II 思維題

https://leetcode.com/problems/jump-game-ii/ 這道題很不錯,我的一種程式碼感覺本質上跟Ans一樣,但是TLE....因為我的寫法還是會有重複 思路一:DP 倒過來看,dp[lastIdx-1]=0, dp[i] = min(1+

LeetCode - 55. Jump Game45. Jump Game II、403. Frog Jump - 維陣列跳躍問題 (多種方法)

55 - Jump Game  -  Medium 45 - Jump Game II  -  Hard 403 - Frog Jump - Hard 上邊三題都是一維陣列中的DP演算法題,一起總結一下: 55 - Jump

45. Jump Game II跳躍遊戲II

問題描述 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the a