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:

nums: [,,]

Result: [,] (of course, [,] will also be ok)

Example 2:

nums: [,,,]

Result: [,,,]

思路: DP。

假设有一个集合,集合内的数两两可除。现在有一个新的数,则该数能加入这个集合中的条件是:集合里最小的数能被这个数整除,或者这个数能够被数组中最大的数整除。

例子:假设集合为{4, 8, 16}, 现在有2,因此4能被2整除,因此2能加入这个数组。如果是32,则32能被16整除,因此也可以加入这个数组。

这道题,我们先把nums数组升序排序。

创建一个数组count[i]表示nums[i]到nums.back()这些数中,包含nums[i]的最大可除集合的大小。显然,nums[i]是这个最大可除集合中的最小的数。

根据上面判断一个新元素能否加入集合中的判断方法,count[i] = max{1 + count[j]} (i < j 且 nums[j] % nums[i] == 0)

同时我们维护另一个数组parent[i]表示从nums[i]到nums.back()这些数中,包含nums[i]的最大可除集合中的第二大的数。初始时这个parent[i] = i。

最后,我们维护一个head指针,它在计算过程中始终指向当前已知的最大可除集合中的最小值。然后通过parent数组我们就能遍历这个集合中的所有的数。

算法时间复杂度O(n^2),空间复杂度为O(n)。

 class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> res;
if (nums.size() == ) return res;
vector<int> count(nums.size(), );
vector<int> parent(count);
int head = , maxLen = ;
sort(nums.begin(), nums.end(), less<int>());
for (int i = nums.size() - ; i >= ; i--) {
parent[i] = i;
for (int j = i + ; j < nums.size(); j++) {
if (nums[j] % nums[i] == && count[i] < + count[j]) {
count[i] = + count[j];
parent[i] = j;
if (maxLen < count[i]) {
maxLen = count[i];
head = i;
}
}
}
}
res.push_back(nums[head]);
while (head != parent[head]) {
head = parent[head];
res.push_back(nums[head]);
}
return res;
}
};

Largest Divisible Subset -- LeetCode的更多相关文章

  1. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  2. LeetCode "Largest Divisible Subset" !

    Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...

  3. 【LeetCode】368. Largest Divisible Subset 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...

  4. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  5. 【leetcode】368. Largest Divisible Subset

    题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...

  6. [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  7. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. 368 Largest Divisible Subset 最大整除子集

    给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...

随机推荐

  1. 【BZOJ 3643】Phi的反函数 数搜索

    这道题是典型的数搜索,讲究把数一层一层化小,而且还有最重要的大质数剪枝. #include <cstdio> #include <cmath> typedef long lon ...

  2. ubuntu下opencv使用cvNamedWindow()和cvShowImage()出错的解决方法

    重装系统和opencv,编译运行显示一副图像的程序,报错如下 liurf@liurf-Lenovo-G470:~/WorkSpace/slambook-master/ch5/imageBasics$ ...

  3. -webkit-overflow-scrolling:touch;

    -webkit-overflow-scrolling建了带有硬件加速的系统级控件,所以效率很高.但是这相对是耗更多内存的,最好在产生了非常大面积的overflow时才应用. 而且在 ios8  里有b ...

  4. 解决在极光推送的时候会出现一个 JPush提示:缺少统计代码

    <span style="font-size:14px;"> @Override protected void onResume(){ super.onResume() ...

  5. 【BZOJ2818】Gcd [莫比乌斯反演]

    Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...

  6. 【CF1027E】Inverse Coloring(DP)

    题意:给出一个n*n的矩阵,要求在每个位置涂上黑/白色, 要求满足:任意相邻的两行,其颜色要么完全相同,要么完全相反 任意相邻的两列,其颜色也要么相同要么完全相反 且这个矩形中,不存在任意一个大小大于 ...

  7. 智能合约安全-parity多重签名钱包安全漏洞

    漏洞原因: 因为initWallet函数是公开函数( public function) , 攻击者调用initWallet,重新初始化钱包会把之前合约钱包所有者覆盖, 即可改变钱包所有者. 漏洞代码: ...

  8. Appium===Appium+Python API(转)

    Appium+python自动化8-Appium Python API 前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts contex ...

  9. 【bzoj3089】gty的二逼妹子序列

    一眼又是个莫队-- 首先看这时间/空间复杂度,线段树/主席树就别想了-- 然后么--zcy就有点傻了-- 于是zcy看了下hzwer,感觉受教育了. 分块的调块大小真是玄学设计. 有没有一种方法在修改 ...

  10. [ python3 ] 基于zabbix 自动抓取每天监控数据

    通过python登录到zabbix直接抓取每天的数据的图片趋势图,并制作成静态index.html给与展示并发送提示邮件. 操作系统:Centos6.7 python版本:python3.5 #!/u ...