1269. Number of Ways to Stay in the Same Place After Some Steps(medium)

  1. Number of Ways to Stay in the Same Place After Some Steps
    You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place (The pointer should not be placed outside the array at any time).
    Given two integers steps and arrLen, return the number of ways such that your pointer still at index 0 after exactly steps steps.
    Since the answer may be too large, return it modulo 10^9 + 7.

1.动态规划的数组设置参考官方题解,dp[i][j]表示走j步到达i有多少种方案。
转移关系:走j步到达i的方案有,j-1步时在i-1步,需要向右走;j-1步时在i+1步,需要向左走;j-1步时在i步,原地不动。所以dp[i][j]=dp[i-1][j-1]+dp[i][j-1]+dp[i+1][j-1].

细节:

  • 计算结果可能很大,所以对每次的加法结果都要取模。由于运算只涉及加法,所以取模的位置为最终结果无影响
  • 关于dp的size,arrlen>steps时,无法走到大于steps的部分,所以取dp的第一维为min(steps,arrlen)
    int numWays(int steps, int arrLen) {
        if(steps==1||arrLen==1){
            return 1;
        }
        int n=arrLen;
        if(n>steps){
            n=steps;
        }
        vector<vector<int>>f;
        f.resize(n,vector<int>(steps));
        for(int i=0;i<n;i++){
            f[i][i]=1;
        }
        for(int i=1;i<steps;i++){
            f[0][i]=(f[0][i-1]+f[1][i-1])%(1000000007);
            for(int j=1;i+j<steps&&j<n-1;j++){
                f[j][i+j]=((f[j-1][i+j-1]+f[j][i+j-1])%(1000000007)+f[j+1][i+j-1])%(1000000007);
                
            }
            if(n-1+i<steps){
                f[n-1][n-1+i]=(f[n-1][n-2+i]+f[n-2][n-2+i])%(1000000007);
            }
           
        }
        return (f[0][steps-1]+f[1][steps-1])%(1000000007);
    }

2.参考题解重写代码使更简洁

int numWays(int steps, int arrLen) {
        if(steps==1||arrLen==1){
            return 1;
        }
        int n=min(arrLen,steps);
        vector<vector<int>>f;
        f.resize(steps,vector<int>(n));
        f[0][0]=1;
        for(int i=1;i<steps;i++){
            for(int j=0;j<=i&&j<n;j++){
                f[i][j]=f[i-1][j];
                if(j-1>=0){
                    f[i][j]=(f[i][j]+f[i-1][j-1])%(1000000007);
                }
                if(j+1<=i&&j+1<n){
                    f[i][j]=(f[i][j]+f[i-1][j+1])%(1000000007);
                }
            }
        }

        return (f[steps-1][0]+f[steps-1][1])%(1000000007);
    }

3.官方题解
动态规划只涉及两行数据,所以可以只记录这两行

    int numWays(int steps, int arrLen) {
        if(steps==1||arrLen==1){
            return 1;
        }
        int n=min(arrLen,steps);
        vector<int>pre(n);
        vector<int>cur(n);
        pre[0]=1;
        for(int i=1;i<steps;i++){
            for(int j=0;j<=i&&j<n;j++){
                cur[j]=pre[j];
                if(j-1>=0){
                    cur[j]=(cur[j]+pre[j-1])%(1000000007);
                }
                if(j+1<=i&&j+1<n){
                    cur[j]=(cur[j]+pre[j+1])%(1000000007);
                }
            }
            pre=cur;
        }

        return (cur[0]+cur[1])%(1000000007);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值