题目
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 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [1,2,4,8] |
思路
DP.
状态转移方程: dp[x] = max(dp[x], dp[y] + 1) 其中: 0 <= y < x 且 nums[x] % nums[y] == 0
再通过一个辅助数组pre回溯。
代码
1 | class Solution(object): |