Last active
January 20, 2020 19:38
-
-
Save adamkorg/449195846ecee84226c94f8a02a0d51d to your computer and use it in GitHub Desktop.
Leetcode 62: Unique Paths - iterative 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 uniquePaths(int m, int n) { | |
if (m == 0 || n == 0) return 0; | |
vector<vector<int>> dp(n, vector<int>(m, 1)); | |
for (int r=1; r<n; ++r) { | |
for (int c=1; c<m; ++c) | |
dp[r][c] = dp[r-1][c] + dp[r][c-1]; | |
} | |
return dp[n-1][m-1]; | |
} | |
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