LeetCode 题解(49): Jump Game II
来源:互联网 发布:万人迷网络电话软件 编辑:程序博客网 时间:2024/04/30 03:22
题目:
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 is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
题解:题目不难,麻烦在决定下一步应该从哪跳。假设到位置 i 至少需要S[i] 步,并设S[0] = 0 (第一个位置不需要跳即可到)。
情况1:若从当前位置可以直接跳到末尾,直接S[i] + 1
情况2:若从当前位置跳不到末尾,先检查 A[i + A[i]] + (i + A[i]) 是否可以到最后一个位置(也即从当前位置能跳到的最远的位置,再从该位置能否跳到末尾)。如果可以到末尾,不需多做计算,直接返回S[i] + 2。
情况3:若不满足情况1和情况2,则挨个检查从当前位置能跳到的所有元素,决定下一次的起跳点。决定的原则是:下一个起跳点应该能跳的更远,所以取任意位置j,使得 j+ A[j]最大即可。
C++版:
class Solution {public: int jump(int A[], int n) { if(!n || (!A[0] && n > 1)) return -1; int *mini = new int[n]; for(int i = 0; i < n; i++) { mini[i] = std::numeric_limits<int>::max(); } mini[0] = 0; for(int j = 0; j < n - 1;) { if(j + A[j] >= n - 1) return mini[j] + 1; else if(A[j+A[j]] + j + A[j] >= n - 1) return mini[j] + 2; int max = 0, index = 0; for(int k = j + 1; k <= j + A[j]; k++) { if(A[k] + k >= max) { max = A[k] + k; index = k; } mini[k] = min(mini[k], mini[j] + 1); } if(!index) j++; else j = index; } return mini[n-1]; } };
Java版
public class Solution { public int jump(int[] A) { if(A.length == 0 || (A[0] == 0 && A.length > 1)) return -1; int[] current_min = new int[A.length]; current_min[0] = 0; for(int i = 0; i < A.length - 1;) { if(A[i] + i >= A.length - 1) return current_min[i] + 1; int current_max = 0, index = 0; for(int k = i + 1; k <= i + A[i]; k++) { if(k + A[k] >= A.length - 1) return current_min[i] + 2; else { if(k + A[k] >= current_max) { current_max = k + A[k]; index = k; } } current_min[k] = current_min[i] + 1; } if(index == 0) i++; else i = index; } return current_min[A.length-1]; }}
Python版:
class Solution: # @param A, a list of integers # @return an integer def jump(self, A): if len(A) == 0 or (A[0] == 0 and len(A) > 1): return -1 current_min = [0] * len(A) i = 0 while i < len(A) - 1: if i + A[i] >= len(A) - 1: return current_min[i] + 1 current_max = 0 index = 0 for j in range(i+1, i+A[i]+1): if j + A[j] >= len(A) - 1: return current_min[i] + 2 else: if j + A[j] >= current_max: current_max = j + A[j] index = j current_min[j] = current_min[i] + 1 if index == 0: i += 1 else: i = index return current_min[len(A)-1]
- LeetCode 题解(49): Jump Game II
- leetcode Jump Game II 题解
- LeetCode 55. Jump Game and 45. Jump Game II 题解
- LeetCode 55. Jump Game & 45. Jump Game II题解
- LeetCode 45 Jump Game II 题解
- leetcode题解-45. Jump Game II
- LeetCode题解:Jump Game
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- [leetcode]Jump Game II
- LeetCode-Jump Game II
- Ubuntu中文输入法的安装
- 我的词典:即粘即用一
- Android SpeechRecognizer语音识别
- [Leetcode] Combination Sum
- hadoop2.0yarn框架简介
- LeetCode 题解(49): Jump Game II
- 盒子模型
- Unity使用杂记
- C# WinForm窗体任意位置点击事件
- log4j使用方法及实例
- 朴素的UNIX之-进程/线程模型
- C# 键盘快捷键
- GCD并发线程数量控制技术
- 3.将maven项目jar纳入maven仓库,Mave项目依赖另外一个Maven项目的案例