[Week 11] LeetCode 403. Frog Jump

15 篇文章 0 订阅
13 篇文章 0 订阅

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.

示例

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.

题解

先理解一下题意,如果我们在某刻以 k 单元距离跳跃到 m 上,那么我们下一次跳跃的距离只能是 k-1、k 和 k+1。很重要的一点时,一开始你是处在 0 这个位置,而你的第一跳一定要跳到 1 上,这个条件就让我们有一个很好的限制:不用考虑一开始的 k 是多少。所以我们的思路如下:

在某刻以 k 单元距离跳跃到 m 上,那么我们下一次跳跃的距离只能是 k-1、k 和 k+1,则我们就判断这三跳是否能跳到合法的位置。这就要求我们记录能从上一块石头跳到这块石头的所有跳跃距离。那么接下来不妨尝试写一个状态转移方程,其中 canJump(i) 表示能否跳达 i:

// k < i, j < len(dist[k])
canJump(i) = OR(canJump(k) && (
	(dist[k][j] + k) == i) ||
	(dist[k][j] + k - 1) == i) ||
	(dist[k][j] + k + 1) == i) ||
); 
dist[i].insert...

验证一下是否正确:

因为 k < i,所以 canJump(k)dist[k][j] 都能在它之前算出来,但是有一个问题,如果我们每访问一个石头,我们就要遍历它前面的所有石头,那么总的复杂度会达到 O(n3)。能不能做得更好呢?

实际上,我们关心的只是上一次跳多远可以达到这块石头,而在你遍历前面石头的过程中,会有很多重复的距离,例如1->2->3->51->3->5dist[5] 里只会记录一次 2。那我们不如用 set 来作为 dist 的数据结构。同时我们转变一下思路,从头遍历每块石头,然后判断 k-1、k、k+1 距离跳跃能否跳到后面的石头上,然后记录后面这块石头的跳跃距离。

看完代码思路应该会很清晰:

代码

class Solution {
public:
  bool canCross(vector<int> &stones) {
    unordered_map<int, unordered_set<int> > dp;
    for (auto i : stones)
      dp[i] = unordered_set<int>();
    dp[0].insert(0);

    for (auto i : stones) {
      for (auto k : dp[i]) {
        if (k - 1 > 0 && dp.find(i + k - 1) != dp.end())
          dp[i + k - 1].insert(k - 1);
        if (dp.find(i + k) != dp.end())
          dp[i + k].insert(k);
        if (dp.find(i + k + 1) != dp.end())
          dp[i + k + 1].insert(k + 1);
      }
    }

    return !dp[stones[stones.size() - 1]].empty();
  }
};

复杂度

两层循环,其中遍历 dp[i]) 复杂度我们可以认为是 O(n),所以复杂度一目了然,为 O(n2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值