题目:
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.)
题意:
给定一个非负整数数组,给定的初始化位置在数组的起始位置。
数组中的每个元素代表着你能都在此位置跳跃的最大的距离。
你的目标是用最少的跳跃数达到数组的末尾。
比如:给定A = [2,3,1,1,4]
达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)
算法分析:
该题思想主要是,扫描数组,以确定当前最远能覆盖的节点,放入maxreach。然后继续扫描,直到当前的路程超过了上一次算出的覆盖范围reach,那么更新覆盖范围,同时更新条数,因为我们是经过了多一跳才能继续前进的。
形象地说,这个是在争取每跳最远的greedy.
* ret:目前为止的jump数
* curRch:从A[0]进行ret次jump之后达到的最大范围
* curMax:从0~i这i+1个A元素中能达到的最大范围
* 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此需要增加一次jump,使之达到
* 记录的curMax。
AC代码:
/**
* ret:目前为止的jump数
* curRch:从A[0]进行ret次jump之后达到的最大范围
* curMax:从0~i这i+1个A元素中能达到的最大范围
* 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此需要增加一次jump,使之达到
* 记录的curMax。
*/
public class Solution
{
public int jump(int[] nums)
{
int ret = 0;
int curMax = 0;
int curRch = 0;
for(int i = 0; i < nums.length; i ++)
{
if(curRch < i)
{
ret ++;
curRch = curMax;
}
curMax = Math.max(curMax, nums[i]+i);
}
return ret;
}
}
本文介绍了一个算法问题,即如何在给定的非负整数数组中找到从起始位置到达末尾所需的最少跳跃次数。通过逐步解析算法思路,提供了一种有效的方法来解决这个问题,并附带详细的代码实现。
1598

被折叠的 条评论
为什么被折叠?



