diff --git a/leetcode/src/1137.c b/leetcode/src/1137.c index 0bb7fdc38a..511b643f32 100644 --- a/leetcode/src/1137.c +++ b/leetcode/src/1137.c @@ -1,7 +1,4 @@ -// Dynamic Programming -// Runtime: O(n) -// Space: O(1) -int tribonacci(int n){ +int tribonacci(int n) { int t0 = 0; int t1 = 1; int t2 = 1; @@ -10,19 +7,19 @@ int tribonacci(int n){ return t0; } - if (n == 1){ + if (n == 1) { return t1; } - if (n == 2){ + if (n == 2) { return t2; } - for (int i = 0; i < n - 2; i++){ - int nextT = t0 + t1 + t2; + for (int i = 3; i <= n; i++) { + int nextTerm = t0 + t1 + t2; t0 = t1; t1 = t2; - t2 = nextT; + t2 = nextTerm; } return t2;