Skip to content

Instantly share code, notes, and snippets.

@cashlo
Created August 6, 2018 07:42
Show Gist options
  • Save cashlo/078c62521b9d68799194125f05f53281 to your computer and use it in GitHub Desktop.
Save cashlo/078c62521b9d68799194125f05f53281 to your computer and use it in GitHub Desktop.
class Solution(object):
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums: return []
from_big_nums = sorted(nums)
dp = []
for i,n in enumerate(from_big_nums):
# print dp
last_divisiable = [s for s in dp if n % s[-1] == 0]
if last_divisiable:
dp.append(max(last_divisiable, key=len) + [n])
else:
dp.append([n])
# print dp
return max(dp, key=len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment