[LeetCode] 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 is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
int jump(int A[], int n)
{
int pos = 0;
int nextPos = 0;
int count = 0;
while (true)
{
pos = nextPos;
int span = 0;
for (int j = 1; j <= A[pos]; j++)
{
int temp = pos + j;
if (temp+A[temp] > span)
{
nextPos = temp;
span = temp + A[temp];
}
}
count++;
if (nextPos >= n-1)
{
return count;
}
}
}
相關文章
- [LeetCode] 45. Jump Game IILeetCodeGAM
- [LeetCode] 55. Jump GameLeetCodeGAM
- 【leetcode】45. Jump Game II 非負陣列的最少跳躍步數LeetCodeGAM陣列
- Jump-gameGAM
- Jump Game(C++)GAMC++
- 【Leetcode】1340. Jump Game V 【動態規劃/記憶性搜尋】LeetCodeGAM動態規劃
- 55-jump Game 跳躍遊戲GAM遊戲
- Leetcode 292. Nim GameLeetCodeGAM
- LeetCode 403 Frog Jump 青蛙過河 Javascirpt 解決方案LeetCodeJava
- 【Leetcode】1690. Stone Game VIILeetCodeGAM
- leetcode-90. Subsets IILeetCode
- Leetcode 40 Combination Sum IILeetCode
- Leetcode 213 House Robber IILeetCode
- LeetCode 1103[分糖果II]LeetCode
- [LeetCode] 210. Course Schedule IILeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- [LeetCode] 212. Word Search IILeetCode
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode] 2105. Watering Plants IILeetCode
- LeetCode 52. N皇后 IILeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- leetcode 219. Contains Duplicate IILeetCodeAI
- [LeetCode] 910. Smallest Range IILeetCode
- LeetCode-047-全排列 IILeetCode
- Leetcode 137. Single Number IILeetCode
- [LeetCode] 3152. Special Array IILeetCode
- Leetcode 685. Redundant Connection II JavascriptLeetCodeJavaScript
- [LeetCode] 244. Shortest Word Distance IILeetCode
- LeetCode - 113 - 路徑總和 IILeetCode
- leetcode 350. Intersection of Two Arrays IILeetCode
- leetcode力扣 213. 打家劫舍 IILeetCode力扣
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode-063-不同路徑IILeetCode
- leetcode 679. 24 Game(遊戲24點)LeetCodeGAM遊戲
- leetcode:組合總和II(回溯java)LeetCodeJava
- LeetCode題解(0407):接雨水II(Python)LeetCodePython
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- [Leetcode]下一個更大元素IILeetCode
- Leetcode——113. 路徑總和 IILeetCode