题目:
题解:
class Solution:
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
n = len(nums)
nums.sort()
dp = [1]*n
res = [[nums[i]] for i in range(n)]
ans = res[0]
for i in range(n):
for j in range(i):
if nums[i] % nums[j] == 0 and dp[j] + 1 > dp[i]:
dp[i] = dp[j] + 1
res[i] = res[j] + [nums[i]]
if len(res[i]) > len(ans):
ans = res[i]
return ans