All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1101_1200.s1137_n_th_tribonacci_number.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g1101_1200.s1137_n_th_tribonacci_number

// #Easy #Dynamic_Programming #Math #Memoization #Dynamic_Programming_I_Day_1
// #2023_05_31_Time_122_ms_(69.35%)_Space_33.6_MB_(64.52%)

class Solution {
    fun tribonacci(n: Int): Int {
        return if (n == 0) {
            0
        } else if (n <= 2) {
            1
        } else {
            var tn = 0
            var tn1 = 1
            var tn2 = 1
            var tmp = 0
            for (i in 3..n) {
                tmp = tn + tn1 + tn2
                tn = tn1
                tn1 = tn2
                tn2 = tmp
            }
            tmp
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy