1 class Solution(object):
 2     def largestDivisibleSubset(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: List[int]
 6         """
 7         # The container that holds all intermediate solutions.
 8         # key: the largest element in a valid subset.
 9         subsets = {-1: set()}
10         
11         for num in sorted(nums):
12             subsets[num] = max([subsets[k] for k in subsets if num % k == 0], key=len) | {num}
13         
14         return list(max(subsets.values(), key=len))

参考:https://leetcode-cn.com/problems/largest-divisible-subset/solution/zui-da-zheng-chu-zi-ji-by-leetcode/

posted on 2020-04-09 10:27  Sempron2800+  阅读(132)  评论(0编辑  收藏  举报