您的位置:首页 > 博客中心 > 互联网 >

动态规划-停在原地的方案数

时间:2022-05-11 10:56

 

题目

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

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps

 

首先想的是递归。超时了

class Solution2 {
        /*
            steps:总移动步数
            arrLen:数组长度
            有多少种方案移动后还在0位置
            可以+1 -1 0
            距离0最远arrlen-1
            +1和-1 的步数一定是相同的否则无分发回到起点
            超时
        */
        public int numWays(int steps, int arrLen) {
            return (int)(dfs(0,0,arrLen,steps)%Math.round((Math.pow(10,9)+7)));
        }

        // 深度优先遍历:应该会超时
        public long dfs(int c_step, int index, int arrLen, int steps){
            if(c_step ==steps){
                if(index == 0){
                    return 1;
                }
                return 0;
            }
            long all_step = 0;
            // 往右,往左,不动
            long r,l,s;
            // 最右
            if(index == arrLen-1) {
                s = dfs(c_step + 1, index, arrLen,steps);
                l = dfs(c_step + 1, index - 1, arrLen,steps);
                all_step += (s+l);
            }
            // 最左
            else if(index == 0) {
                s = dfs(c_step + 1, index, arrLen,steps);
                r = dfs(c_step + 1, index + 1, arrLen,steps);
                all_step += (s+r);
            }
            else {
                s = dfs(c_step + 1, index, arrLen,steps);
                l = dfs(c_step + 1, index - 1, arrLen,steps);
                r = dfs(c_step + 1, index + 1, arrLen,steps);
                all_step += (s+r+l);
            }

            return  all_step;
        }
    }

 

动态规划

 

初始化:dp[][] = new int[steps+1][maxCol+1]  (maxCol = Math.min(steps,arrLen-1))

  dp[0][0]:当前没有走,且当前在0位置的走法,1中

含义:dp[i][j]:当前走了i步,索引位置在j,数值:走法

状态转移:dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + dp[i-1][j+1] (有边界情况)

 

class Solution {
        /*
            动态规划:
            dp[steps][index]:steps当前走了多少步,index当前的位置,有多少种走法
        */
        public int numWays(int steps, int arrLen) {
            final int MOD = 1000000007;

            int maxCol = Math.min(arrLen-1, steps);
            int[][] dp = new int[steps+1][maxCol+1];
            dp[0][0] = 1;
            for(int i=1; i<=steps; i++){
                for(int j=0; j<=maxCol; j++){
                    // 不动的步数
                    dp[i][j] = dp[i-1][j];
                    // 往右走
                    if(j>=1){
                        dp[i][j] = (dp[i][j]+dp[i-1][j-1])%MOD;
                    }
                    if(j<=maxCol-1){
                        dp[i][j] = (dp[i][j]+dp[i-1][j+1])%MOD;
                    }
                    System.out.print(dp[i][j] +" ");
                    try {
                        TimeUnit.MILLISECONDS.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println();
            }
            return dp[steps][0];
        }
    }

 

本类排行

今日推荐

热门手游