资讯专栏INFORMATION COLUMN

[LeetCode] 403. Frog Jump

赵连江 / 2971人阅读

Problem

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 < 2^31.
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.
Solution
class Solution {
    public boolean canCross(int[] stones) {
        if (stones == null || stones.length < 2) return true;
        if (stones[0] != 0 || stones[1] != 1) return false;
        Map> map = new HashMap<>();
        for (int stone: stones) {
            map.put(stone, new HashSet<>());
        }
        map.get(0).add(1);
        int len = stones.length;
        for (int stone: stones) {
            for (int step: map.get(stone)) {
                if (stone+step == stones[len-1]) return true;
                Set nextSet = map.get(stone+step);
                if (nextSet != null) {
                    if (step-1 > 0) nextSet.add(step-1);
                    nextSet.add(step);
                    nextSet.add(step+1);
                }
            }
        }
        return false;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72070.html

相关文章

  • leetcode403. Frog Jump

    摘要:石头的位置用整数数组来表示。广度优先遍历可以从起点开始,对从该点出发的所有可能步数进行遍历,并更新从该点可达的点的所有的可行步数。利用数据结构来记录该结果,其中的为的数,它的对应的是从该出发的所有的上一步的步长。 题目要求 A frog is crossing a river. The river is divided into x units and at each unit the...

    Soarkey 评论0 收藏0
  • [leetcode] 403. Frog Jump

    摘要:还有一个石头可能由之前的多个石头到达,这又是可以优化的地方。当前结果可由之前的结果得出,且有重复的搜索方法,就需要用。 [链接描述]leetcode 题目。 有点类似于jump game, 只不过这里对步数有了隐形的要求,当前步数和之前步数有关。如果我们用brute force的方法来解,每一步有三种可能,一共n个石头,时间复杂度就是O(3^n)。这其中有很多步数是多余的,第i个石头...

    mo0n1andin 评论0 收藏0
  • leetcode45. Jump Game II

    摘要:转换为广度优先算法即为我们只需要找到每一步的开始节点和结束下标,找到下一轮遍历的最大下标,如果该下标超过了数组长度,那么结束遍历,返回步数,否则将上一轮的最终节点加一作为起始节点,并将下一轮最大下标最为结束节点,继续遍历。 题目要求 Given an array of non-negative integers, you are initially positioned at the ...

    shiguibiao 评论0 收藏0
  • LeetCode[45] Jump Game II

    摘要:复杂度思路每次设置一个窗口,观察在这一步下能到达的最远距离,不断的移动这个窗口。计数,需要移动几次,才能覆盖到末尾的值。 LeetCode[45] Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Eac...

    keelii 评论0 收藏0
  • [LintCode/LeetCode] Jump Game I & II

    摘要:建立动规数组,表示从起点处到达该点的可能性。循环结束后,数组对所有点作为终点的可能性都进行了赋值。和的不同在于找到最少的步数。此时的一定是满足条件的最小的,所以一定是最优解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...

    rose 评论0 收藏0

发表评论

0条评论

赵连江

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<