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

题意:

一只不会游泳的青蛙,从位置0th unit(即我画的图中的第0号石头)出发开始跳,

若前一次它跳了K units,

这次就只能跳 K-1 或者 K 或者 K+1 units,

问青蛙是否能跳到对岸。

思路:

以[0,1,3,5,6,8,12,17]为例自己画了个图,我把units理解为直尺上的刻度,更直观理解题意。

用HashMap套一个set,set内存放last jump的units

首先,遍历一遍给定数组stones = {0,1,3,5,6,8,12,17}, 存入所有给定刻度,初始化HashMap。

Integer(当前刻度)        0    1    3    5   6   8   12   17

Set(last jump units)    []  []    []    []  []   []    []    [] 

然后,初始化0刻度对应的Set为[0]。

Integer(当前刻度)         0    1    3    5   6   8   12   17

Set(last jump units)    [0]  []    []    []  []   []    []    [] 

最后,遍历一遍给定数组stones = {0,1,3,5,6,8,12,17}

根据题意,青蛙的下一跳(nextJump)范围取决于上一跳(lastJump)

遍历Set里上一跳的取值

Integer(当前刻度)         0    1   3   5   6   8   12   17

Set(last jump units)    [0]  []   []   []  []   []    []    [] 

lastJump                 0

nextJump                 -1

                         0

                         1  

查找map.containsKey(下一跳的刻度),即 (当前刻度 + nextJump Range)

Integer(当前刻度)         0    1    3    5      6        8        12          17

Set(last jump units)    [0]   [1]    [2]     [2]      [3,1]         [3,2]          [4]                [5] 

lastJump                 0      1       2       2        3   1          3   2           4                    5

nestJump Range:

(1)lastJump-1           -1      0        1       1        2     0        2      1        3        

(2)lastJump              0      1        2       2        3     1        3      2        4       

(3)lastJump+1            1      2        3       3        4     2        4      3        5       

map.containsKey         0-1?    1+0?     3+1?    5+1?   6+2?  6+0?      8+2?  8+1?      12+3?

                        0+0?    1+1?     3+2?    5+2?   6+3?  6+1?      8+3?  8+2?      12+4?

                        0+1?    1+2?    3+3?    5+3?   6+4?  6+2?      8+4? 8+3?       12+5?

代码:

 1  public boolean canCross(int[] stones) {
 2         if (stones.length == 0) return false;
 3         HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
 4         for (int i = 0; i < stones.length; i++) {
 5             map.put(stones[i], new HashSet<>());
 6         }
 7
 8         map.get(0).add(0);
 9         for (int i = 0; i < stones.length; i++) {
10             for (int lastJump : map.get(stones[i])) {
11                 for (int nextJump = lastJump - 1; nextJump <= lastJump + 1; nextJump++) {
12                     if (nextJump > 0 && map.containsKey(stones[i] + nextJump)) {
13                         map.get(stones[i] + nextJump).add(nextJump);
14                     }
15                 }
16             }
17         }
18         return !map.get(stones[stones.length - 1]).isEmpty();
19     }

原文地址:https://www.cnblogs.com/liuliu5151/p/9114677.html

时间: 2024-10-10 02:43:53

[leetcode]403. Frog Jump青蛙过河的相关文章

403 Frog Jump 青蛙过河

一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上). 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2).如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1.k 或 k + 1个单位. 另请注意,青蛙只能向前方(终

[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(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]]=

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

趣味算法——青蛙过河(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号的青蛙上面. 你的任务是