Skip to content

Commit 9f57139

Browse files
committed
Time: 5 ms (42.73%), Space: 6 MB (17.60%) - LeetHub
1 parent 9af5755 commit 9f57139

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution {
22
public:
3-
int rec(int i) {
3+
int rec(int i, vector<int> &dp) {
44
if(i <= 1) return i;
5-
return rec(i - 1) + rec(i - 2);
5+
if(dp[i] != -1) return dp[i];
6+
return dp[i] = rec(i - 1, dp) + rec(i - 2, dp);
67
}
78

89
int fib(int n) {
9-
return rec(n);
10+
vector<int> v(n + 1, -1);
11+
return rec(n, v);
1012
}
1113
};

0 commit comments

Comments
 (0)