[LintCode] Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.

Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.

If the frog's last jump was k units, then its next jump must be either k - 1k, or k + 1 units. Note that the frog can only jump in the forward direction.

 Notice
  • The number of stones is ≥ 2 and is < 1100.
  • Each stone's position will be a non-negative integer < 2^31.
  • The first stone's position is always 0.
Example
Given stones = [0,1,3,5,6,8,12,17]

There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. The frog can jump to the last stone by jumping 
1 unit to the 2nd stone, then 2 units to the 3rd stone, then 
2 units to the 4th stone, then 3 units to the 6th stone, 
4 units to the 7th stone, and 5 units to the 8th stone.

Given stones = `[0,1,2,3,4,8,9,11]`

Return false. There is no way to jump to the last stone as 
the gap between the 5th and 6th stone is too large.


这道题这个题意很难懂,其实是说第一个点是0, 第二个点是1,然后下面开始,每一次都可以跳上一次多跳的步数k/k+1/k-1步
eg. 0 -> 1 k = 0 + 1
  1 -> 3 k = 1 + 1 (3 - 2 =1)
  3 -> 5 k = 2 + 0
  5 -> 8 k = 2 + 1
  8 -> 12 k = 3 + 1
  12 -> 17 k = 4 + 1
思路一:
动态规划,想到动态规划是因为这是求状态的问题,同时给定的数据顺序有关系。需要注意的是下一次可以跳的步数,直接与上一次能跳的步数有关。在这里面我们设计一个map,其实key是stone的值,value是一个set,里面是存了该值可以前进的步数。
然后我们依次循环除了最后的一个stones,每一个stones都拿出来得到它可以前进的步数,在这个stone的基础上加上步数(k/k+1/k-1),如果可以跳到其中的一个stone,我们需要把这个步数存下来,因为下一次这个点就可以在这个的基础上跳(k/k+1/k-1)
最后就是看最后一个stone的set是不是空的,如果是空的就说明前面没有石头能跳到这一步。
public class Solution {
    /*
     * @param stones: a list of stones' positions in sorted ascending order
     * @return: true if the frog is able to cross the river or false
     */
    public boolean canCross(int[] stones) {
        // write your code here
        if (stones == null || stones.length == 0) {
            return false;
        }
        
        Map<Integer, Set<Integer>> dp = new HashMap<Integer, Set<Integer>>();
        for (int i = 0; i < stones.length; i++) {
            dp.put(stones[i], new HashSet<Integer>());
        }
        dp.get(0).add(0);
        
        for (int i = 0; i < stones.length - 1; i++) {
            int stone = stones[i];
            for (Integer k : dp.get(stone)) {
                if (k - 1 > 0 && dp.containsKey(stone + k - 1))
                    dp.get(stone + k - 1).add(k - 1);
                if (dp.containsKey(stone + k)) 
                    dp.get(stone + k).add(k);
                if (dp.containsKey(stone + k + 1))
                    dp.get(stone + k + 1).add(k + 1);
            }
        }
        return !dp.get(stones[stones.length - 1]).isEmpty();
    }
}

思路二

就是用dfs来做,因为可以看成不停的搜素,先所有的点都用k+1来试,不行就用k,然后k-1。

http://blog.csdn.net/YABAJ/article/details/77115969

posted on 2018-02-02 12:08  codingEskimo  阅读(244)  评论(0编辑  收藏  举报

导航