Created
September 27, 2019 14:38
-
-
Save adamkorg/30af9b024d10c3cf156d1fd69c8fb0ef to your computer and use it in GitHub Desktop.
Leetcode 62: Unique Paths - recursive O(n!)
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
#include <iostream> | |
using namespace std; | |
void walk(int x, int y, int sizex, int sizey, int& count) { | |
//end condition is x,y are bottom right, so count++ and return | |
if (x == sizex-1 && y == sizey-1) { | |
count++; | |
return; | |
} | |
//attempt to go down if room | |
if (y < sizey-1) | |
walk(x, y+1, sizex, sizey, count); | |
//attempt to walk right if room | |
if (x < sizex-1) | |
walk(x+1, y, sizex, sizey, count); | |
} | |
int uniquePaths(int m, int n) { | |
int count = 0; | |
walk(0, 0, m, n, count); | |
return count; | |
} | |
int main() { | |
cout << uniquePaths(23, 12) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment