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]
思路:
用动态规划,但是动规的方程好难想,dp[i]代表以nums[i]为最大元素的序列最多有多少个。
然后只要nums[k]%nums[i]=0,则可尝试往dp[k]转移。
代码:
![](https://img.haomeiwen.com/i18019269/a9907137587cf9d1.png)
网友评论