forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2183 from AkifhanIlgaz/1137
Create: 1137-n-th-tribonacci-number
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
func main() { | ||
|
||
} | ||
|
||
func tribonacci(n int) int { | ||
t := [3]int{0, 1, 1} | ||
|
||
if n < 3 { | ||
return t[n] | ||
} | ||
|
||
for i := 3; i < n+1; i++ { | ||
t[0], t[1], t[2] = t[1], t[2], sum(t) | ||
} | ||
|
||
return t[2] | ||
} | ||
|
||
func sum(arr [3]int) int { | ||
total := 0 | ||
|
||
for _, val := range arr { | ||
total += val | ||
} | ||
|
||
return total | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var tribonacci = function (n) { | ||
let t = [0, 1, 1]; | ||
|
||
if (n < 3) return t[n]; | ||
|
||
for (let i = 3; i < n + 1; i++) { | ||
[t[0], t[1], t[2]] = [t[1], t[2], sum(t)]; | ||
} | ||
|
||
return t[2]; | ||
}; | ||
|
||
function sum(arr) { | ||
return arr.reduce((a, b) => a + b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
impl Solution { | ||
pub fn tribonacci(n: i32) -> i32 { | ||
let mut t = [0, 1, 1]; | ||
|
||
if n <= 2 { | ||
return t[n as usize]; | ||
} | ||
|
||
for i in 3..(n + 1) as usize { | ||
t.swap(0, 1); | ||
t.swap(1, 2); | ||
t[2] = t.iter().sum(); | ||
} | ||
|
||
t[2] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function tribonacci(n: number): number { | ||
let t = [0, 1, 1]; | ||
|
||
if (n < 3) return t[n]; | ||
|
||
for (let i = 3; i < n + 1; i++) { | ||
[t[0], t[1], t[2]] = [t[1], t[2], sum(t)]; | ||
} | ||
|
||
return t[2]; | ||
} | ||
|
||
function sum(arr: number[]): number { | ||
return arr.reduce((a, b) => a + b); | ||
} |