-
Notifications
You must be signed in to change notification settings - Fork 3
/
1137-n-th-tribonacci-number.php
115 lines (94 loc) · 2.29 KB
/
1137-n-th-tribonacci-number.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
// 1137. N-th Tribonacci Number
// https://leetcode.com/problems/n-th-tribonacci-number/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 0 ms, faster than 100.00% of PHP online submissions
// for N-th Tribonacci Number.
// Memory Usage: 14.7 MB, less than 100.00% of PHP online submissions
// for N-th Tribonacci Number.
// class Solution
// {
// /**
// * @param Integer $n
// * @return Integer
// */
// function tribonacci($n)
// {
// list($x, $y, $z) = [0, 1, 1];
// if ($n < 3) return [$x, $y, $z][$n];
// for ($i = 3; $i < $n; $i++) list($x, $y, $z) = [$y, $z, $x + $y + $z];
// return $x + $y + $z;
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 8 ms, faster than 50.00% of PHP online submissions
// for N-th Tribonacci Number.
// Memory Usage: 14.6 MB, less than 100.00% of PHP online submissions
// for N-th Tribonacci Number.
class Solution
{
/**
* @param Integer $n
* @return Integer
*/
function tribonacci($n)
{
$a = [0, 1, 1];
for ($i = 3; $i <= $n; $i++) $a[$i] = $a[$i - 3] + $a[$i - 2] + $a[$i - 1];
return $a[$n];
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// t0 = 0 + 1 + 1 = 0
// t1 = 0 + 1 + 1 = 1
// t2 = 0 + 1 + 1 = 1
// t3 = 0 + 1 + 1 = 2
// t4 = 1 + 1 + 2 = 4
// t5 = 1 + 2 + 4 = 7
// t6 = 2 + 4 + 7 = 13
$tests = [
[
'input' => 0,
'expected' => 0,
],
[
'input' => 1,
'expected' => 1,
],
[
'input' => 2,
'expected' => 1,
],
[
'input' => 3,
'expected' => 2,
],
[
'input' => 4,
'expected' => 4,
],
[
'input' => 5,
'expected' => 7,
],
[
'input' => 6,
'expected' => 13,
],
[
'input' => 25,
'expected' => 1389537,
],
];
$s = new Solution();
foreach ($tests as $test) {
extract($test);
$result = $s->tribonacci($input);
if ($expected === $result) {
echo "✅ {$input}<br>";
} else {
echo "🔴 {$input}<br>";
echo "<i>Expected <b>{$expected}</b>, but got <b>{$result}</b></i><br>";
}
echo '<hr>';
}