368. Largest Divisible Subset
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 49% Topics: Array, Math, Dynamic Programming, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n^2)
# Space: O(n)
class Solution(object):
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums:
return []
nums.sort()
dp = [1] * len(nums)
prev = [-1] * len(nums)
largest_idx = 0
for i in xrange(len(nums)):
for j in xrange(i):
if nums[i] % nums[j] == 0:
if dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
prev[i] = j
if dp[largest_idx] < dp[i]:
largest_idx = i
result = []
i = largest_idx
while i != -1:
result.append(nums[i])
i = prev[i]
return result[::-1]
Solution from kamyu104/LeetCode-Solutions · MIT