Js the number of different paths

Shortest Path
Ideas:

  1. If it is only one line or a situation, it would have only one path
  2. If the diagonal position, there is at first a right rear (start-> A-> C) and the lower rear to right (start-> B-> C) two pathways as shown below:
    Here Insert Picture Description
  3. And so on, we can conclude that the total number of paths each path is the path to the left position below the plus
    Here Insert Picture Description
function short(m, n) {
    var i,j;
	let dp=new Array(n).fill(0).map(x=>new Array(m).fill(0));
	for(i=0; i<m; i++) {
		dp[0][i]=1;
	}
	for(i=0; i<n; i++) {
		dp[i][0]=1;
	}
	for(i=1; i<n; i++){
		for(j=1; j<m; j++){
			dp[i][j]=dp[i-1][j]+dp[i][j-1];
		}	
	}
	return dp[n-1][m-1];	

}
console.log(short(7,3));
Published 46 original articles · won praise 12 · views 10000 +

Guess you like

Origin blog.csdn.net/yuanfangyoushan/article/details/104044614
Recommended