python có trò swap này đấy fenPython: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
t0, t1, t2 = t1, t2, ans
Thanks fence, mình xài Python ngu vlpython có trò swap này đấy fen
Python:t0, t1, t2 = t1, t2, ans
Với python thì fen nên đọc quyển fluent python 3rd. Viết hay vãi, code rất đẹp, đọc vỗ đùi đen đét luô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.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
Thank Bác, mấy bài dạng DP khó quá, em chỉ làm được mấy bài dễ thôiĐú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.
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;
};
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];
}
}
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;
}
}
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];
}
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]
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]
mấy fen đăng sol python lên ngó trộm ko hiểu code@LmaoSuVuong có thù gì với Python à, gạch tôi suốt
Học dễ bỏ mẹ. Đợt mới đi làm, đọc 1 tuần là đc việc rồi. Mỗi tội thư viện nhiều nên phải làm nhiều cho quen, còn ngôn ngữ có một nhúmmấy fen đăng sol python lên ngó trộm ko hiểu code
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);
}
}
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]
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]