Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Last active January 20, 2020 19:38
Show Gist options
  • Save adamkorg/449195846ecee84226c94f8a02a0d51d to your computer and use it in GitHub Desktop.
Save adamkorg/449195846ecee84226c94f8a02a0d51d to your computer and use it in GitHub Desktop.
Leetcode 62: Unique Paths - iterative DP
#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