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 - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.

Note:

The number of stones is ≥ 2 and is < 1,100.
Each stone's position will be a non-negative integer < 231.
The first stone's position is always 0.
Example 1:

[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.
Example 2:

[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.

答案

最简单的recursion答案

    public boolean canCross(int[] stones) {
        for(int st : stones) stones_pos.add(st);
        boolean ret = recur(stones, 0, 0);
        return ret;
    }

    public boolean recur(int[] stones, int i, int last_k) {
        if(!stones_pos.contains(i)) return false;
        if(i > stones[stones.length - 1]) return false;
        if(i == stones[stones.length - 1]) return true;
        boolean ret = false;
        /*
        * Currently, frog is on stone i, and it can jump k - 1, k or k+1 steps forward
        * Let's try all options using recursion
        * */

        // Jump k - 1
        if(last_k - 1 > 0)
            ret = ret || recur(stones, i + (last_k - 1), last_k - 1);
        if(ret) return true;

        // Jump k
        if(last_k != 0)
            ret = recur(stones, i + last_k, last_k);
        if(ret) return true;

        // Jump k + 1
        ret = recur(stones, i + last_k + 1, last_k + 1);
        if(ret) return true;
        return false;
    }
}

dp答案

class Solution {
    Map stones_pos = new HashMap<>();
    int[][] dp = new int[1100][1100];
    
    /*
    *
    * dp[i][j] := frog is standing on stone position i, and last jump was j, can frog jump to the last stone in the end ?
    *
    * */
    public boolean canCross(int[] stones) {
        int id = 0;
        for(int st : stones) stones_pos.put(st, id++);

        for(int i = 0; i < dp.length; i++)
            Arrays.fill(dp[i], -1);
        
        boolean ret = recur(stones, 0, 0);
        return ret;
    }

    public int indexof(int pos) {
        return stones_pos.get(pos);
    }

    public boolean recur(int[] stones, int i, int last_k) {
        if(!stones_pos.containsKey(i)) return false;
        if(i > stones[stones.length - 1]) return false;
        if(i == stones[stones.length - 1]) return true;
        if(dp[indexof(i)][last_k] != -1) return (dp[indexof(i)][last_k] == 1) ? true:false;
        boolean ret = false;

        /*
        * Currently, frog is on stone i, and it can jump k - 1, k or k+1 steps forward
        * Let's try all options using recursion
        * */

        // Jump k - 1
        if(last_k - 1 > 0)
            ret = ret || recur(stones, i + last_k - 1, last_k - 1);
        if(ret) return true;

        // Jump k
        if(last_k != 0)
            ret = recur(stones, i + last_k, last_k);
        if(ret) return true;

        // Jump k + 1
        ret = recur(stones, i + last_k + 1, last_k + 1);

        dp[indexof(i)][last_k] = ret?1:0;
        return ret;
    }
}

你可能感兴趣的:(Frog Jump)