Created
November 8, 2017 08:31
-
-
Save jianminchen/baeda48097123edd7f4c78519d64e9f5 to your computer and use it in GitHub Desktop.
Catalan number - peer performed Java code -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
class Solution { | |
static int numOfPathsToDest(int n) { | |
// your code goes here | |
int [][] numPaths = new int[n][n]; | |
int sumNeighbours; | |
numPaths[n - 1][0] = 1; // set as 1 // i, j, think out loud -> talk | |
for (int i = n - 1; i >= 0; i--) { // i, j does not -> meaningful name horizontal/ vertical | |
for (int j = n - i - 1; j < n; j++) { // left / top - 0, 0, left/bottom 0, 0 | |
sumNeighbours = 0; // | |
if (j > 0) { | |
sumNeighbours += numPaths[i][j - 1]; | |
} | |
if (i < n - 1) { | |
sumNeighbours += numPaths[i + 1][j]; | |
} | |
numPaths[i][j] += sumNeighbours; // 1, structure -> white board | |
} | |
} | |
return numPaths[0][n - 1]; // do not write debug statement -> whiteboard, every line | |
} | |
public static void main(String[] args) { | |
} | |
} | |
/* | |
j 0 1 2 3 4 | |
i | |
0 [ x x x x 14 ] excellent alaysis | |
1 [ x x x 5 14 ] | |
2 [ x x 2 5 9 ] | |
3 [ x 1 2 3 4 ] | |
4 [ 1 1 1 1 1 ] where is i where j , where is (0,0) - (n -1, 0) | |
starting from row 4 - line 38 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment