thảo luận Leetcode mỗi ngày

Hết graph, sang DP rồi đấy

C++:
int tribonacci(int n) {
    if (n == 0) return 0;
    if (n == 1 || n == 2) return 1;
    vector<int> dp(n+1, 0);
    dp[1] = dp[2] = 1;
    for(int i = 3; i <= n; ++i)
        dp[i] = dp[i-1] + dp[i-2] + dp[i-3];
    return dp[n];
}
 
Last edited:
Python:
class Solution:
    def tribonacci(self, n: int) -> int:
        if n == 0:
            return 0

        if n == 1 or n == 2:
            return 1

        t0 = 0
        t1 = 1
        t2 = 1
        ans = 0
        for i in range(3, n + 1):
            ans = t0 + t1 + t2
            t0 = t1
            t1 = t2
            t2 = ans

        return ans
python có trò swap này đấy fen
Python:
t0, t1, t2 = t1, t2, ans
 
Bài này sao em thấy các bác dùng mảng để chứa các giá trị nhỉ, dùng 3 biến để swap có nhanh hơn không ạ? Cảm ơn các bác
Đúng là nhanh hơn, nhưng bài này kiểu kinh điển của DP một chiều. Trong DP một chiều thì dùng mảng để lưu các tính toán trước đó. Bài này dạng đặc biệt hơn nên dùng được 3 biến.
 
Đúng là nhanh hơn, nhưng bài này kiểu kinh điển của DP một chiều. Trong DP một chiều thì dùng mảng để lưu các tính toán trước đó. Bài này dạng đặc biệt hơn nên dùng được 3 biến.
Thank Bác, mấy bài dạng DP khó quá, em chỉ làm được mấy bài dễ thôi =((
 
lại 1 tuần DP chăng
JavaScript:
function tribonacci(n: number): number {
        if (n < 2) return n;
        let v0 = 0, v1 = 1, v2 = 1, vT = 0;
        while (n-- > 2) {
            vT = v0 + v1 + v2;
            v0 = v1;
            v1 = v2;
            v2 = vT;
        }
        return v2;
};
 
Java:
class Solution {
    public int tribonacci(int n) {
     
        int[] tribonacci = new int[38];
        tribonacci[0]=0;
        tribonacci[1]=1;
        tribonacci[2]=1;

        for(int i =3;i<=n;i++){
            tribonacci[i]=tribonacci[i-1]+tribonacci[i-2]+tribonacci[i-3];
        }
        return tribonacci[n];
    }
}
làm graph chưa đã mà
YHs5f6H.png
 
Queue
Java:
class Solution {
    public int tribonacci(int n) {
        List<Integer> q = new LinkedList<>();
        q.addAll(List.of(0, 1, 1));
        int result = 0;

        if (n < 3) return q.get(n);

        while (n - 3 >= 0) {
            result = sumQueue(q);
            q.remove(0);
            q.add(result);
            n--;
        }

        return result;
    }

    private int sumQueue(List<Integer> q) {
        int sum = 0;

        for (int element: q) {
            sum += element;
        }

        return sum;
    }
}
 
Java:
    public int tribonacci(int n) {
        if(n == 0) return 0;
        if(n <= 2) return 1;
        int[] dp = new int[n+1];
        dp[1] = dp[2] = 1;
        for(int i = 3; i <= n; i++){
            dp[i] = dp[i-1] + dp[i-2] + dp[i-3];
        }
        return dp[n];
    }
 
Python:
class Solution:
    def tribonacci(self, n: int) -> int:
        if n == 0:
            return 0
        x, y, z = 0, 1, 1
        for i in range(3, n+1):
            x, y, z = y, z, x+y+z
        return z
 
Python:
class Solution:
    def tribonacci(self, n: int) -> int:
        T = [0] * 38
        T[1] = 1
        T[2] = 1
        
        for i in range (3, n+1):
            T[i] = T[i-3] + T[i-2] + T[i-1]
            
        return T[n]
Python:
class Solution:
    def tribonacci(self, n: int) -> int:
        return [0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415, 223317, 410744, 755476, 1389537, 2555757, 4700770, 8646064, 15902591, 29249425, 53798080, 98950096, 181997601, 334745777, 615693474, 1132436852, 2082876103][n]
 
PHP:
class Solution {

    /**
     * @param Integer $n
     * @param Integer $nth
     * @param Integer $n1
     * @param Integer $n2
     * @param Integer $n3
     * @return Integer
     */
    function tribonacci($n, $nth=3, $n1=0, $n2=1, $n3=1) {
        if ($n == 0) return 0;
        if ($n == 1 || $n == 2) return 1;

        $s = $n1 + $n2 + $n3;
        if ($nth == $n) return $s;
        
        return $this->tribonacci($n, $nth+1, $n2, $n3, $s);
    }
}
 
Python:
class Solution:
    def tribonacci(self, n: int) -> int:
        lst = [0,1,1]
        if n == 0:
            return 0
        for i in range(2,n):
            t = sum(lst)
            lst.append(t)
            lst.pop(0)
        return lst[-1]
 
Python:
class Solution:
    def tribonacci(self, n: int) -> int:
        if n==0:
            return 0
        elif n==1 or n==2:
            return 1
        dp = [0,1,1]
        for i in range(3,n+1):
            dp[0],dp[1],dp[2] = dp[1], dp[2], dp[0]+dp[1]+dp[2]
        return dp[-1]
 
Back
Top