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.

算法分析

这道题的约束比较多,用了相同的算法改了三次才通过。
简单说一下我的算法,我的算法其实十分简单,就是构建一个数组来储存跳到目前位置的最后一条的步数。
比如说,对于[0,1,2,3]来说

ans[0] = { 0, 0, 0, 0}
ans[1] = { 1, 0, 0, 0}
ans[2] = { 0, 1, 0, 0}
ans[3] = { 0, 2, 1, 0}

这个数组应该很容易理解,就是对于第 i 行来说,第 j 列的值就代表从第 j 个石头跳到第 i 个石头步数。按照这个逻辑,我们一定可以通过第 i -1 行之前的石头求出 第 i 个 石头的所有跳法。即

for each i do
if stones[j] == ans[i][k]+ stones[i] + (-1/0/1)
【表示:如果能从 第 i个石头以某种可行步数跳到 第 j 个石头,那么记录下步数】
then ans[j][i] = stones[i] - stones[j]
【表示:从第 i 个石头 跳到 第 j 个石头最后一次的步数】

因为这里如果微观来看,ans[i][j]的意义在变化,但是如果从总的角度来看,实质上ans[i][j]储存的就是在 i 石头上最后一次跳 j 了步。

按照这个想法可以写出来

bool canCross(vector<int>& stones) {
      vector<vector<int> >   ans(stones.size(),vector<int>(stones.size()));
      if(stones[1]!=1) return false;
      else ans[0][1] = 1;       
      if(stones.size()<=2) return true;
      for(int i = 1 ; i < stones.size();i++){
                for(int k=0 ; k < i ; k++){
            if(ans[k][i]<=0) continue;
            else{
                int canReach = ans[k][i]+stones[i];
                for(int j = i+1 ; j < stones.size() ;j++){
                    if(stones[j]<canReach-1) continue;
                    if(stones[j]>canReach+1) break;
                    if(canReach-1 == stones[j]) ans[i][j] = canReach-1 - stones[i];
                    if(canReach== stones[j]) ans[i][j] = canReach - stones[i];
                    if(canReach+1 == stones[j]) ans[i][j] = canReach+1 - stones[i];                 
                }
            }
        }
        if(ans[i][ans.size()-1]>0) return true;

      }

    return false;
} 

但是这个并没有通过,因为内存不够用,二维数组开得太大了,实际上我们可以发现,我们基本上没用储存很多数据,这道题应该是一个稀疏的矩阵,然后我就改了一下,变成了动态的矩阵,只需要一直往里面push就可以了。但是这样又有一个问题,在一些测试数据过不了,比如说[0,1,2,3, ……, 999]这组数据过不了,因为一直往里面PUSH会有组合数的产生,数字十分巨大,所以最后我用了set这种数据结构来储存,可以避免重复即避免排列组合的产生。(实际上固定大小的二维数组可以避免这个问题,折衷来说,这种数据结构可以融合前两种数据结构的优点)
上一种做法和这一组的做法在本质上是一样的时间复杂度都是O(N^2),但由于矩阵稀疏再加上不搜索距离大于最大可达目标的点,实际上要远小于O(N^2)这个时间复杂度。
代码如下:

bool canCross(vector<int>& stones) {
      vector<set<int> >   ans(stones.size());
      if(stones[1]!=1) return false;
      else ans[1].insert(1);
      if(stones.size()<=2) return true;

      for(int i = 1 ; i < stones.size();i++){
                for(set<int>::iterator k=ans[i].begin(); k !=ans[i].end() ; k++){

                int canReach = *k +stones[i];
                for(int j = i+1 ; j < stones.size() ;j++){
                    if(stones[j]<canReach-1) continue;
                    if(stones[j]>canReach+1) break;
                    ans[j].insert(stones[j]-stones[i]);

                }
            }
        if(ans[ans.size()-1].size()>0){
            return true;
        } 


    }           
    return false;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值