nums
in ascending order.dp
of the same length as nums
, where dp[i]
represents the size of the largest divisible subset ending with nums[i]
.prev
to keep track of the previous element in the subset for reconstruction.nums[i]
starting from the second element:
nums[j]
where j < i
, check if nums[i] % nums[j] == 0
.dp[j] + 1 > dp[i]
, update dp[i]
to dp[j] + 1
and set prev[i]
to j
.dp
, which indicates the end of the largest subset.prev
array, starting from the index of the maximum value in dp
.