Last active
January 20, 2020 19:37
-
-
Save adamkorg/ad667076ab09fea88b79abed95c2bed6 to your computer and use it in GitHub Desktop.
Leetcode 62: Unique Paths - recursive DP
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> | |
#include <vector> | |
using namespace std; | |
int count(int rows, int cols, int r, int c, vector<vector<int>>& dp) { | |
if (r == rows || c == cols) return 0; | |
if (r == rows-1 && c == cols-1) return 1; | |
if (dp[r][c] != -1) return dp[r][c]; | |
dp[r][c] = count(rows, cols, r+1, c, dp) + count(rows, cols, r, c+1, dp); | |
return dp[r][c]; | |
} | |
int uniquePaths(int m, int n) { | |
vector<vector<int>> dp(n, vector<int>(m, -1)); | |
return count(n, m, 0, 0, dp); | |
} | |
int main() { | |
int m = 7, n = 3; | |
cout << uniquePaths(m, n) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment