Skip to content

Commit 909762f

Browse files
add 1137
1 parent 777a5cd commit 909762f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|5087|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5087.java) | O(1) | O(1) | |Medium||
3131
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
32+
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
3233
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
3334
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | O(m*n) | O(1) | |Easy||
3435
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1137. N-th Tribonacci Number
5+
*
6+
* The Tribonacci sequence Tn is defined as follows:
7+
*
8+
* T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
9+
*
10+
* Given n, return the value of Tn.
11+
*
12+
* Example 1:
13+
* Input: n = 4
14+
* Output: 4
15+
*
16+
* Explanation:
17+
* T_3 = 0 + 1 + 1 = 2
18+
* T_4 = 1 + 1 + 2 = 4
19+
*
20+
* Example 2:
21+
* Input: n = 25
22+
* Output: 1389537
23+
*
24+
* Constraints:
25+
* 0 <= n <= 37
26+
* The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.
27+
* */
28+
public class _1137 {
29+
public static class Solution1 {
30+
public int tribonacci(int n) {
31+
if (n <= 1) {
32+
return n;
33+
}
34+
int[] numbers = new int[n + 1];
35+
numbers[0] = 0;
36+
numbers[1] = 1;
37+
numbers[2] = 1;
38+
for (int i = 3; i <= n; i++) {
39+
numbers[i] = numbers[i - 1] + numbers[i - 2] + numbers[i - 3];
40+
}
41+
return numbers[n];
42+
}
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1137;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1137Test {
10+
private static _1137.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1137.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.tribonacci(3));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(4, solution1.tribonacci(4));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(1389537, solution1.tribonacci(25));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0, solution1.tribonacci(0));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(1, solution1.tribonacci(2));
40+
}
41+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy