Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created September 27, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkorg/30af9b024d10c3cf156d1fd69c8fb0ef to your computer and use it in GitHub Desktop.
Save adamkorg/30af9b024d10c3cf156d1fd69c8fb0ef to your computer and use it in GitHub Desktop.
Leetcode 62: Unique Paths - recursive O(n!)
#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