美文网首页
603. Largest Divisible Subset

603. Largest Divisible Subset

作者: 鸭蛋蛋_8441 | 来源:发表于2019-07-23 07:02 被阅读0次

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]转移。

代码:

相关文章

网友评论

      本文标题:603. Largest Divisible Subset

      本文链接:https://www.haomeiwen.com/subject/uvjnlctx.html