title : The only path (robot walking squares)
Difficulty : Medium
topic content :
A robot is located at the Top-left corner of a m x n grid (marked ' Start ' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying-to-reach the bottom-right corner of the grid (marked ' Finish ' in the diagram below).
How many possible unique paths is there?
translation :
The robot is located in the upper-left corner of the M x N grid (labeled "Start" in).
The robot can move or move to the right at any point in time. The robot is trying to reach the lower-right corner of the grid (labeled "Finish" in).
How many possible paths are there?
Note: m and N would be is at most 100.
Example 1:
Input:m = 3, n = 2output:3
Example 2:
Input:m = 7, n = 3output:28
my train of thought : Dynamic programming problems, each point (m,n) is the sum of the possible numbers of the two points (M-1,n) + (m,n-1). The simplest way is to implement it with recursion.
My Code :
1 Public int uniquepaths (intint n) {2 if (m = = 1 | | n==1) { 3 return 1; 4 }5 return uniquepaths (m,n-1) + uniquepaths (m-1, n); 6 }
my complexity : O (m*n) spatial complexity is also O (m*n)--recursion depth
Results: 41/62 test Cases passed. Time Limit exceeded
Last executed input:51 9
Recursion is like this, the code is simple, but it runs very slowly, and a little bit larger will time out.
Answer code :
1 Public intUniquepaths (intXinty) {2 intDp[][] =New int[x][y];3 for(inti = 0; i< x;i++){4 for(intj = 0;j<y;j++){5 if(i = = 0 | | j = 0) {6DP[I][J] = 1;7 Continue;8 }9DP[I][J] = Dp[i-1][j] + dp[i][j-1];Ten } One } A returnDp[x-1][y-1]; -}
complexity of the answer: O (m*n) spatial complexity is also O (m*n) Although the complexity is the same, but because of the iteration instead of recursion, the speed of operation greatly increased.
The answer : Recursion is nothing more than the calculation of this layer to call a deep layer of computation, and then the result of a deep layer returned, so if the first layer from the top to the last level of all the values are calculated and saved by a data structure, then you can directly iterate to calculate.
So one-dimensional recursion : one-dimensional only with one-dimensional correlation, so the straight line iteration can be. For example: factorial (only related to the previous, record one can), Fibonacci sequence (two related, need to record two)
Two-dimensional recursion : Each element is related to two dimensions, so you have to borrow a two-dimensional array to record all of the values.
extension : when x = = Y, this is a positive square, is there a better way?
Because it is similar to Cattleya number at this time, there is f (n+1) =f (n) * (4*n-2), so adding square judgment in the code can optimize the calculation speed of the square, as follows:
if (x = = y) {int ans =1 ; for (int i = 1; i < x; i++) { = ans * (4*i-2)/i; } return ans;
Note : When you want to use the "*=" symbol, if the right side is an expression and contains division sign, then it is best not to use it, because this operator is the first to compute the right and then multiply itself, so it is possible that the right side of the calculation order is wrong.
For example when X==y==4 is at this time ans = =6 and I ==3, if using ans *= (4*i-2)/ I, so first calculate to the right there is 10/3 = 3, and then multiply by 6 The last result is 18, the result is wrong.
Instead of using ans = ans * (4*i-2)/I, there will be no such error.
Leetcode [62] (Java): Unique Paths and extensions