403 Frog Jump 青蛙过河

一只青蛙想要过河。 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有)。 青蛙可以跳上石头,但是不可以跳入水中。
给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上)。 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2)。
如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1、k 或 k + 1个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。
请注意:
    石子的数量 ≥ 2 且 < 1100;
    每一个石子的位置序号都是一个非负整数,且其 < 231;
    第一个石子的位置永远是0。
示例 1:
[0,1,3,5,6,8,12,17]
总共有8个石子。
第一个石子处于序号为0的单元格的位置, 第二个石子处于序号为1的单元格的位置,
第三个石子在序号为3的单元格的位置, 以此定义整个数组...
最后一个石子处于序号为17的单元格的位置。
返回 true。即青蛙可以成功过河,按照如下方案跳跃:
跳1个单位到第2块石子, 然后跳2个单位到第3块石子, 接着
跳2个单位到第4块石子, 然后跳3个单位到第6块石子,
跳4个单位到第7块石子, 最后,跳5个单位到第8个石子(即最后一块石子)。
示例 2:
[0,1,2,3,4,8,9,11]
返回 false。青蛙没有办法过河。
这是因为第5和第6个石子之间的间距太大,没有可选的方案供青蛙跳跃过去。
详见:https://leetcode.com/problems/frog-jump/description/

C++:

class Solution {
public:
    bool canCross(vector<int>& stones) {
        unordered_map<int, unordered_set<int>> m;
        vector<int> dp(stones.size(), 0);
        m[0].insert(0);
        int k = 0;
        for (int i = 1; i < stones.size(); ++i)
        {
            while (dp[k] + 1 < stones[i] - stones[k])
            {
                ++k;
            }
            for (int j = k; j < i; ++j)
            {
                int t = stones[i] - stones[j];
                if (m[j].count(t - 1) || m[j].count(t) || m[j].count(t + 1))
                {
                    m[i].insert(t);
                    dp[i] = max(dp[i], t);
                }
            }
        }
        return dp.back() > 0;
    }
};

参考:https://www.cnblogs.com/grandyang/p/5888439.html

原文地址:https://www.cnblogs.com/xidian2014/p/8855140.html

时间: 2024-08-02 20:38:27

403 Frog Jump 青蛙过河的相关文章

[leetcode]403. Frog Jump青蛙过河

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 ord

[LeetCode] Frog Jump 青蛙过河

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 ord

[LeetCode] 403. Frog Jump 青蛙跳

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 ord

Leetcode-403 Frog Jump(青蛙过河)

1 #define pb push_back 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 class Solution 4 { 5 public: 6 bool canCross(vector<int>& stones) 7 { 8 int sz = stones.size(); 9 if(sz==2&&stones[1]==1) return true; 10 11 vector<set<

403. Frog Jump

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 ord

第十七周 Leetcode 403. Frog Jump(HARD) 线性dp

leetcode403 我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护. 总的复杂度O(n^2logn) class Solution { public: bool canCross(vector<int>& stones) { map<int,int>po; int n=stones.size(); map<int,int>dis[1500]; for(int i=0;i<stones.size();i++) po[stones[i]]=

趣味算法——青蛙过河(JAVA)

青蛙过河是一个非常有趣的智力游戏,其大意如下: 一条河之间有若干个石块间隔,有两队青蛙在过河,每队有3只青蛙,这些青蛙只能向前移动,不能向后移动,且一次只能有一只青蛙向前移动.在移动过程中,青蛙可以向前面的空位中移动,不可以一次跳过两个位置,但是可以跳过对方一只青蛙进入到前面的一个空位.问两队青蛙该如何移动才能用最少的步数分别走向对岸?( → → → □ ← ← ← )可能3只青蛙太少了,心算也不难.如果有100只青蛙呢? /** * 青蛙过河 * @author rubekid * */ pu

HLG 1584 青蛙过河 (二分)

链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1584 Description 青蛙王国一年一度的游戏又开始了,这个游戏要求青蛙必须跳过河.河的宽度是 L .河里有n块石头,这n块石头从河的一边笔直的连到另一边.青蛙只能踩着石头过河,如果它们掉到水里,将被淘汰出局.游戏规定青蛙最多跳m次.现在青蛙想要知道如果在这m步内跳到岸的那边,它一步最长需要跳多长. Input 输入包括多组测试

P1244 青蛙过河

P1244 青蛙过河 题目描述 有一条河,左边一个石墩(A区)上有编号为1,2,3,4,-,n的n只青蛙,河中有k个荷叶(C区),还有h个石墩(D区),右边有一个石墩(B区),如下图所示.n只青蛙要过河(从左岸石墩A到右岸石墩B),规则为: (1)石墩上可以承受任意多只青蛙,荷叶只能承受一只青蛙(不论大小): (2)青蛙可以:A→B(表示可以从A跳到B,下同),A→C,A→D,C→B,D→B,D→C,C→D: (3)当一个石墩上有多只青蛙时,则上面的青蛙只能跳到比它大1号的青蛙上面. 你的任务是