成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 403. Frog Jump

趙連江 / 3069人閱讀

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://m.hztianpu.com/yun/72070.html

相關(guān)文章

  • leetcode403. Frog Jump

    摘要:石頭的位置用整數(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 the...

    Soarkey 評論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條評論

趙連江

|高級講師

TA的文章

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