Skip to content

Instantly share code, notes, and snippets.

@gitzhou
Created October 31, 2014 15:56
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 gitzhou/b9c078dbe7741f7d63d8 to your computer and use it in GitHub Desktop.
Save gitzhou/b9c078dbe7741f7d63d8 to your computer and use it in GitHub Desktop.
f(x, y) = f(x - 1, y) + f(x, y - 1)
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int f(int x, int y) {
vec.resize(x + 1);
for (int i = 0; i < x + 1; ++i) {
vec[i] = vector<int>(y + 1, 0);
vec[i][0] = 2;
}
for (int i = 0; i < y + 1; ++i) {
vec[0][i] = 2;
}
return cal(x, y);
}
int g(int x, int y) {
if (x == 0 || y == 0) {
return 2;
}
return g(x - 1, y) + g(x, y - 1);
}
private:
int cal(int x, int y) {
if (vec[x][y] != 0) {
return vec[x][y];
}
vec[x][y] = cal(x - 1, y) + cal(x, y - 1);
return vec[x][y];
}
private:
vector<vector<int> > vec;
};
int main() {
cout << Solution().f(20, 20) << endl;
cout << Solution().g(15, 15) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment