首页 > 代码库 > [LeetCode]45 Jump Game II

[LeetCode]45 Jump Game II

https://oj.leetcode.com/problems/jump-game-ii/

http://blog.csdn.net/linhuanmars/article/details/21356187

public class Solution {
    public int jump(int[] A)
    {
        // [2, 3, 1, 1, 4]
        // [2, 1, 2, 1, 0]
        
        if (A == null || A.length <= 1)
            return 0;
        
        int len = A.length;
        int start = 0;
        int end = 0;
        int jump = 0;
        while (end < len)
        {
            int maxCanGo = 0;
            jump ++;
            for (int i = start ; i <= end ; i ++)
            {
                int curValue = A[i] + i;
                if (curValue >= len - 1)
                {
                    // Reached
                    return jump;
                }
                
                if (curValue > maxCanGo)
                {
                    maxCanGo = curValue;
                }
            }
            
            if (maxCanGo <= end)
            {
                // Can not go further
                return -1;
            }
            
            start = end + 1;
            end = maxCanGo;
        }
        
        return jump;
    }
}


[LeetCode]45 Jump Game II