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. CF762E Radio Stations

    题目戳这里. 我还以为是KDtree呢,但是KDtree应该也可以做吧. 这是一道数据结构好题.考虑到由于\(K \le 10\),所以我们用两个大vector--\(Left,Right\),\(L ...

  2. 【NOIP模拟赛】书 数学+期望概率

    biubiu~~~ 对于这道傻题.........我考场上退了一个多小时才推出来这个东西是排列...........然后我打的dfs效率n!logInf正好n=9是最后一个能过的数结果前三个点的n全是 ...

  3. watch用法小记

    By francis_hao    Jun 30,2017   watch:周期性的执行一个一个程序,并全屏显示输出 概述 watch [options] command   描述 watch重复的运 ...

  4. POI2007 MEG-Megalopolis [树状数组]

    MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the ...

  5. SCOI2005 互不侵犯 [状压dp]

    题目传送门 题目大意:有n*n个格子,你需要放置k个国王使得它们无法互相攻击,每个国王的攻击范围为上下左走,左上右上左下右下,共8个格子,求最多的方法数 看到题目,是不是一下子就想到了玉米田那道题,如 ...

  6. mybatis的注解功能

    一.mybatis 简单注解 关键注解词 : @Insert : 插入sql , 和xml insert sql语法完全一样 @Select : 查询sql, 和xml select sql语法完全一 ...

  7. 用angular.element实现jquery的一些功能的简单示例

    下面实现了在每个p元素后面自动添加hello world. 在这里我要说的是jquery中的 $document.ready()相当于angualr 中的angualr.element(documen ...

  8. border-1px;避免移动端下边框部分2px

    .border-1px { position: relative; } .border-1px:after { display: block; position: absolute; left:; b ...

  9. HDU2553 N皇后问题---(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2553 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在 ...

  10. HDU1847 Good Luck in CET-4 Everybody!

    大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在 考场浸润了十几载的当代大学生,Kiki和Cici更懂得 ...