力扣笔记 - 停在原地的方案数 (动态规划)

力扣笔记(停在原地的方案数)

点我跳到改题目!

描述: 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处。

每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指针不能被移动到数组范围外)。

给你两个整数 steps 和 arrLen ,请你计算并返回:在恰好执行 steps 次操作以后,指针仍然指向索引 0 处的方案数。

由于答案可能会很大,请返回方案数 模 10^9 + 7 后的结果。

我的思路:回溯判断每一种结果
执行结果 --> 毫无疑问超时

class Solution {
    int count = 0;
    public int numWays(int steps, int arrLen) {
        getCount(0, steps, 0, arrLen);
        return count;
    }
    // 回溯
    /*
        参数:当前是第几步currentStep,总共几步steps,数组长度arrlen, 当前位置currentPosition
        终止条件->所有情况执行完(无)
        全局变量count = 0,可以回到0加一,注意取模
    */
    public void getCount(int currentStep, int steps, int currentPosition,
     int arrLen){
        // 判断是否是最后一步
        if(currentStep == steps){
            if(currentPosition != 0)
                return;
            count++;
            if(count == 1000000007){
                count = 0;
            }
        }else {
        	// 依次走三种情况
            for(int i = 0; i < 3; i++){
                if(i == 0){
                    // 原地
                    getCount(currentStep+1, steps, currentPosition, arrLen);
                }else if (i == 1){
                    // 左走
                    if(currentPosition <= 0)
                        continue;
                    getCount(currentStep+1, steps, currentPosition - 1, arrLen);
                } else{
                    // 右走
                    if(currentPosition >= arrLen-1)
                        continue;
                    getCount(currentStep+1, steps, currentPosition + 1, arrLen);
                }
            }
        }
    }
}

动态规划解法 -->通过

class Solution {
    public int numWays(int steps, int arrLen) {
        /*
            动态规划:
                dp[i][j], 表示走i步后到j位置的情况数。
                则有 dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1],
            因为当前第i步走到了j位置,可能是由三个方向(第i-1步的j-1, j, j+1,这三个位置分别右走,不动, 左走得到)
            而 dp[0][0] = 1 从0开始走0步到达0位置只有一种情况。
               dp[0][j] = 0 (j != 0), 从0开始走0步,不可能到达其他位置。

        */
        // 最大能走到的位置,如果steps比arrlen小,那么不可能走到比steps大的地方,arrlen小同理,节约空间,stepssteps/2 因为还需要一半的步数走回来
        int maxColumn = (arrLen - 1 < steps/2) ? arrLen : steps/2 + 1;
        int[][] dp = new int[steps + 1][maxColumn];
        dp[0][0] = 1;
        int pre, next;
        for(int i = 1; i <= steps; i++){
            for(int j = 0; j < maxColumn; j++){
                // 如果下标越界, 则越界值为0(不可能从该位置到达)
                pre = (j - 1) < 0 ? 0 : dp[i-1][j-1];
                next = (j + 1) >= maxColumn ? 0 : dp[i-1][j+1];
                // 放在三值相加大于int最大值越界
                dp[i][j] = (((pre + next) % 1000000007) + dp[i-1][j]) % 1000000007;
            }
        }
        return dp[steps][0];
    }
}

总结:目前思考问题还是停留在暴力破解上

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值