• 576. Out of Boundary Paths


    Problem statement:

    There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7.

    Example 1:

    Input:m = 2, n = 2, N = 2, i = 0, j = 0
    Output: 6
    Explanation:
    

    Example 2:

    Input:m = 1, n = 3, N = 3, i = 0, j = 1
    Output: 12
    Explanation:
    

    Note:

    1. Once you move the ball out of boundary, you cannot move it back.
    2. The length and height of the grid is in range [1,50].
    3. N is in range [0,50].

    Analysis:

    This question is the last one of leetcode weekly contest 31. Initially, it is tagged with medium, and then adjusted to hard today.

    They mentioned a position in a two dimension board and at most N step to move and count the numbers to get out of boundary. Obviously, DP.

    My first solution:

    Start from (i, j), initialize all the element in the row i and col and j compared their value with N. 

    Do four direction dynamic programming, however, it ignored one fact that the value of one cell can come from all four directions except boundary.

    The answer is wrong. 

    Solution:

    This solution is quite simple, we have m * n board and N step to move, it is a 3 dimension DP. 

    The initialization status: dp[0][0 ... m -1][0 ... n - 1] is 0. means the step is 0, all value is 0.

    Current value only comes from four directions of last move or 1 if it is boundary.

    DP formula is:

    dp[step][row][col] = dp[step - 1][row - 1][col] + dp[step - 1][row + 1][col] + dp[step - 1][row][col - 1] + dp[step - 1][row][col + 1]

    we calculate the value of this three dimension matrix and return the value of dp[N][i][j]. 

    The time complexity is O(N * m * n), space complexity is O((N + 1) * m * n)

    class Solution {
    public:
        int findPaths(int m, int n, int N, int i, int j) {          
            unsigned int dp[N + 1][m][n] = {};
            for(int step = 1; step <= N; step++){
                for(int row = 0; row < m; row++){
                    for(int col = 0; col < n; col++){
                        // the value come from four directoion
                        // if one value comes from boundary: 1
                        //      dp[step - 1][row - 1][col] 
                        //      + dp[step - 1][row + 1][col] 
                        //      + dp[step - 1][row][col - 1] 
                        //      + dp[step - 1][row][col + 1]
                        dp[step][row][col] =  ((row == 0?       1 : dp[step - 1][row - 1][col]) 
                                            + (row == m - 1?    1 : dp[step - 1][row + 1][col])
                                            + (col == 0?        1 : dp[step - 1][row][col - 1])
                                            + (col == n - 1?    1 : dp[step - 1][row][col + 1])) % 1000000007;
                    }
                }
            }
            return dp[N][i][j];
        }
    };
  • 相关阅读:
    趣谈编程史第4期-饱受争议的前端之王JavaScript的血泪成长史
    趣谈编程史第2期-这个世界缺少对C语言的敬畏,你不了解的C语言科普
    趣谈编程史第1期-跌宕起伏的java帝国史,剖析谷歌甲骨文长达8年的版权战争
    记录一次Metaspace扩容引发FGC的调优总结
    多线程学习笔记-深入理解ThreadPoolExecutor
    使用CompletableFuture优化你的代码执行效率
    Linux+Shell常用命令总结
    Guava Cache探索及spring项目整合GuavaCache实例
    将List按照指定大小等分的几种实现方式和效率对比及优化
    Spring的事件机制详解
  • 原文地址:https://www.cnblogs.com/wdw828/p/6823307.html
Copyright © 2020-2023  润新知