国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

leetcode403. Frog Jump

Soarkey / 2324人閱讀

摘要:石頭的位置用整數(shù)數(shù)組來表示。廣度優(yōu)先遍歷可以從起點(diǎn)開始,對從該點(diǎn)出發(fā)的所有可能步數(shù)進(jìn)行遍歷,并更新從該點(diǎn)可達(dá)的點(diǎn)的所有的可行步數(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 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.

假設(shè)有一只青蛙需要過河,河中會(huì)有一些石子,青蛙必須踩在石頭上才算成功。石頭的位置用整數(shù)數(shù)組來表示。青蛙的行走規(guī)則為:假設(shè)上一次青蛙跳了k格,則當(dāng)前青蛙只能跳k-1或k或k+1格,且青蛙只能向前跳,不能向后跳。

廣度優(yōu)先遍歷

可以從起點(diǎn)開始,對從該點(diǎn)出發(fā)的所有可能步數(shù)進(jìn)行遍歷,并更新從該點(diǎn)可達(dá)的點(diǎn)的所有的可行步數(shù)。利用Map>數(shù)據(jù)結(jié)構(gòu)來記錄該結(jié)果,其中map的key為stone的unit數(shù),它的value對應(yīng)的是從該key出發(fā)的所有的上一步的步長。該遍歷思路類似于廣度優(yōu)先遍歷,即將該點(diǎn)出發(fā)遍歷所有的可達(dá)點(diǎn)。代碼如下:

    public boolean canCross(int[] stones) {
        if(stones.length < 2) return true;
        if(stones.length == 2 && stones[1] == 1) return true;
        if(stones.length >= 2 && stones[1] != 1) return false;
        Map> stoneJump = new HashMap<>();
        for(int i = 1 ; i());
        }
        stoneJump.get(1).add(1);
        int finalStone = stones[stones.length-1];
        boolean hasNext = false;
        for(int i = 1 ; i
深度優(yōu)先遍歷

和上一種思路的區(qū)別在于,這種方法會(huì)盡可能往遠(yuǎn)處遍歷。即只要該點(diǎn)可以到達(dá)下一點(diǎn),則會(huì)立刻嘗試從一點(diǎn)到達(dá)終點(diǎn)。代碼如下:

    public boolean canCross(int[] stones) {
        for(int i = 1 ; i i) return false;
        }
        return canCross2(stones, 1, 1);
    }
    
    public boolean canCross2(int[] stones, int idx, int lastStep) {
        if(idx == stones.length-1) return true;
        if(idx < 0 || lastStep <= 0) return false;
        for(int jump = lastStep + 1 ; jump >= lastStep -1 ; jump--) {
            if(canCross2(stones, Arrays.binarySearch(stones, stones[idx] + jump), jump)){
                return true;
            }
        }
        return false;
    }

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

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/74154.html

相關(guān)文章

  • [LeetCode] 403. Frog Jump

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

    趙連江 評論0 收藏0
  • [leetcode] 403. Frog Jump

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

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

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

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

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

    摘要:建立動(dòng)規(guī)數(shù)組,表示從起點(diǎn)處到達(dá)該點(diǎn)的可能性。循環(huán)結(jié)束后,數(shù)組對所有點(diǎn)作為終點(diǎn)的可能性都進(jìn)行了賦值。和的不同在于找到最少的步數(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條評論

最新活動(dòng)
閱讀需要支付1元查看
<