Frog Jump

作者: BLUE_fdf9 | 来源:发表于2018-09-13 05:43 被阅读0次

题目
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<Integer, Integer> 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

    题目A frog is crossing a river. The river is divided into x...

  • Frog Jump

    题目来源青蛙跳的题目,河中有一堆石头,问青蛙能不能跳过河。我一开始想着深度搜索遍历,代码如下: 没错…有超时了…然...

  • 发语音拼读下面单词

    ham bag leg nest list him lost frog jump must

  • [leetcode] Frog Jump

    题目地址A frog is crossing a river. The river is divided into...

  • Leetcode - Frog Jump

    My code: reference:https://discuss.leetcode.com/topic/599...

  • Recursion

    Fibonacci A frog can jump one or two steps at a time. How...

  • [刷题防痴呆] 0403 - 青蛙过河 (Frog Jump)

    题目地址 https://leetcode.com/problems/frog-jump/[https://lee...

  • 403. Frog Jump

    A frog is crossing a river. The river is divided into x u...

  • [snap]frog jump 2D

    instant 2D版frog jump,规定跳跃方向是右或下方。 基本的思路就是依次动态规划,没有什么不同。只是...

  • 8.21 - hard - 78

    403. Frog Jump首先做出来一个TLE的版本,因为这里面要search三种情况,可以用记忆化搜索来做。

网友评论

      本文标题:Frog Jump

      本文链接:https://www.haomeiwen.com/subject/expigftx.html