Skip to content

Commit

Permalink
solutions: 1137 - N-th Tribonacci Number (Easy)
Browse files Browse the repository at this point in the history
- reformat solution 1 code
  • Loading branch information
wingkwong committed Apr 24, 2024
1 parent 2584bdc commit c90f16c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions solutions/1100-1199/1137-n-th-tribonacci-number-easy.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ Output: 1389537
// SC: O(N)
class Solution {
public:
// since first three numbers are given,
// let dp[i] be the value of T_n
// we can build the dp[i] based on dp[i - 1] + dp[i - 2] + dp[i - 3]
// since first three numbers are given,
// let dp[i] be the value of T_n
// we can build the dp[i] based on dp[i - 1] + dp[i - 2] + dp[i - 3]
int tribonacci(int n) {
// first three numbers are known
// first three numbers are known
if (n == 0) return 0;
if (n == 1 || n == 2) return 1;
// dp[i]: the value of T_n
// dp[i]: the value of T_n
vector<int> dp(n + 1);
// base case
// base case
dp[0] = 0;
dp[1] = dp[2] = 1;
// dp[i] is the sum of the previous three values
// dp[i] is the sum of the previous three values
for(int i = 3; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
return dp[n];
}
Expand Down

0 comments on commit c90f16c

Please sign in to comment.