Skip to content

Commit 4d83783

Browse files
authored
Merge pull request neetcode-gh#3106 from nkawaller/n-th-tribonacci-number
Create 1137-n-th-tribonacci-number.cpp
2 parents 6116eb6 + b2bac07 commit 4d83783

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int tribonacci(int n) {
7+
int t[] = {0, 1, 1};
8+
if (n < 3) {
9+
return t[n];
10+
}
11+
for (int i = 3; i <= n; i++) {
12+
int sum_t = t[0] + t[1] + t[2];
13+
t[0] = t[1];
14+
t[1] = t[2];
15+
t[2] = sum_t;
16+
}
17+
return t[2];
18+
}
19+
};

0 commit comments

Comments
 (0)