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

資訊專欄INFORMATION COLUMN

[LeetCode] 403. Frog Jump

趙連江 / 3002人閱讀

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

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/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元查看
<