-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlargest.py
29 lines (25 loc) · 1.11 KB
/
largest.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
from typing import List
class Solution:
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
# Sort nums to ensure that every element is divisible by its previous one if they are in the same subset
nums.sort()
n = len(nums)
# dp[i] stores the size of the largest divisible subset that ends with nums[i]
dp = [1] * n
# prev[i] stores the previous index of nums[i] in the largest divisible subset that ends with nums[i]
prev = [-1] * n
max_size, max_idx = 1, 0
# Build up dp and prev
for i in range(1, n):
for j in range(i):
if nums[i] % nums[j] == 0 and dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
prev[i] = j
if dp[i] > max_size:
max_size, max_idx = dp[i], i
# Reconstruct the largest divisible subset
subset = []
while max_idx != -1:
subset.append(nums[max_idx])
max_idx = prev[max_idx]
return subset[::-1] # Return in ascending order