成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 403. Frog Jump

趙連江 / 3008人閱讀

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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72070.html

相關(guān)文章

  • leetcode403. Frog Jump

    摘要:石頭的位置用整數(shù)數(shù)組來表示。廣度優(yōu)先遍歷可以從起點開始,對從該點出發(fā)的所有可能步數(shù)進行遍歷,并更新從該點可達的點的所有的可行步數(shù)。利用數(shù)據(jù)結(jié)構(gòu)來記錄該結(jié)果,其中的為的數(shù),它的對應(yīng)的是從該出發(fā)的所有的上一步的步長。 題目要求 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

    摘要:還有一個石頭可能由之前的多個石頭到達,這又是可以優(yōu)化的地方。當前結(jié)果可由之前的結(jié)果得出,且有重復(fù)的搜索方法,就需要用。 [鏈接描述]leetcode 題目。 有點類似于jump game, 只不過這里對步數(shù)有了隱形的要求,當前步數(shù)和之前步數(shù)有關(guān)。如果我們用brute force的方法來解,每一步有三種可能,一共n個石頭,時間復(fù)雜度就是O(3^n)。這其中有很多步數(shù)是多余的,第i個石頭...

    mo0n1andin 評論0 收藏0
  • leetcode45. Jump Game II

    摘要:轉(zhuǎn)換為廣度優(yōu)先算法即為我們只需要找到每一步的開始節(jié)點和結(jié)束下標,找到下一輪遍歷的最大下標,如果該下標超過了數(shù)組長度,那么結(jié)束遍歷,返回步數(shù),否則將上一輪的最終節(jié)點加一作為起始節(jié)點,并將下一輪最大下標最為結(jié)束節(jié)點,繼續(xù)遍歷。 題目要求 Given an array of non-negative integers, you are initially positioned at the ...

    shiguibiao 評論0 收藏0
  • LeetCode[45] Jump Game II

    摘要:復(fù)雜度思路每次設(shè)置一個窗口,觀察在這一步下能到達的最遠距離,不斷的移動這個窗口。計數(shù),需要移動幾次,才能覆蓋到末尾的值。 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

    摘要:建立動規(guī)數(shù)組,表示從起點處到達該點的可能性。循環(huán)結(jié)束后,數(shù)組對所有點作為終點的可能性都進行了賦值。和的不同在于找到最少的步數(shù)。此時的一定是滿足條件的最小的,所以一定是最優(yōu)解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...

    rose 評論0 收藏0

發(fā)表評論

0條評論

趙連江

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<