Description
Given a set of?distinct positive?integers, find the largest subset such that every pair?(Si, Sj)?of elements in this subset satisfies:?Si % Sj = 0?or?Sj % Si = 0.
If there are multiple solutions, return any subset is fine.
Example
Example 1:
Input: nums =? [1,2,3],
Output: [1,2] or [1,3]
Example 2:
Input: nums = [1,2,4,8],
Output: [1,2,4,8]
思路:
用動(dòng)態(tài)規(guī)劃,但是動(dòng)規(guī)的方程好難想,dp[i]代表以nums[i]為最大元素的序列最多有多少個(gè)。
然后只要nums[k]%nums[i]=0,則可嘗試往dp[k]轉(zhuǎn)移。
代碼:
