From 9aee0adcdeefd78f14125f14d5fc6742ea0ec753 Mon Sep 17 00:00:00 2001 From: shivamdroidoreo Date: Wed, 2 Oct 2024 15:50:28 +0530 Subject: [PATCH] Update 1137.c --- leetcode/src/1137.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) 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;