-
Notifications
You must be signed in to change notification settings - Fork 43
/
largest-divisible-subset.py
62 lines (58 loc) · 1.61 KB
/
largest-divisible-subset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# V0 : dev
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/83027364
class Solution:
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums: return []
N = len(nums)
nums.sort()
dp = [0] * N #LDS
parent = [0] * N
mx = 0
mx_index = -1
for i in range(N):
for j in range(i - 1, -1 , -1):
if nums[i] % nums[j] == 0 and dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
parent[i] = j
if dp[i] > mx:
mx = dp[i]
mx_index = i
res = list()
for k in range(mx + 1):
res.append(nums[mx_index])
mx_index = parent[mx_index]
return res[::-1]
# V2
# 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 range(len(nums)):
for j in range(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]