Created
October 5, 2018 23:12
-
-
Save hsaputra/d9bf6650eca0f0e890ef3f37c3f879e7 to your computer and use it in GitHub Desktop.
Unique Paths - https://leetcode.com/problems/unique-paths/description/
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
class Solution { | |
public int uniquePaths(int m, int n) { | |
// m is columns | |
// n is rows | |
if (m < 1 || n < 1) { | |
return 0; | |
} | |
if (m == 1 && m == 1) { | |
return 1; | |
} | |
int count = recursiveTraverse(0, 0, n - 1, m - 1, 0); | |
return count; | |
} | |
private int recursiveTraverse(int i, int j, final int maxI, final int maxJ, int count) { | |
if (i == maxI && j == maxJ) { | |
return (count + 1); | |
} | |
if (i > maxI || j > maxJ) { | |
return 0; | |
} | |
int countRight = recursiveTraverse(i, j + 1, maxI, maxJ, count); | |
int countDown = recursiveTraverse(i + 1, j, maxI, maxJ, count); | |
return countRight + countDown; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dynamic programming solution: