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. 停课day5

    一转眼,已经停课五天了. 高二大佬们已经都走了,在机房里面呆着,有时感觉很孤寂. 但是为了能学好竞赛,这些都是在所不惜的. 好像多打打比赛啊,可是cf要FQ,洛谷之类的比赛还不勤. 哎,先去学一发SP ...

  2. HDU 2844 二进制优化的多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. codeforces 1060 C

    https://codeforces.com/contest/1060/problem/C 题意:给你一个长度为n的数列a和长度为m的数列b,定义c(i,j)=ai*bj,得到c矩阵,给定值x,求c矩 ...

  4. bzoj 5094 [Lydsy1711月赛]硬盘检测 概率dp

    [Lydsy1711月赛]硬盘检测 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 273  Solved: 75[Submit][Status][Dis ...

  5. shell之流程控制

    一.if语句.while.for循环各种小例题 1.用户验证 [root@bogon ~]# cat buer.sh #!/bin/bash #user='buer' #password='1234. ...

  6. 全局axios默认值 和 自定义实例默认值

    首先说了一下情况, 登录后成功返回token 然后在带着token去继续下面的请求, 奇怪的是都是当前页面起作用,刷新和跳转之后就token 就消失了. 查了 axios文档发现 被自己坑了 我设置了 ...

  7. linux内核同步之每CPU变量、原子操作、内存屏障、自旋锁【转】

    转自:http://blog.csdn.net/goodluckwhh/article/details/9005585 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 一每 ...

  8. golang命令行参数解析

    package main import ( "fmt" "os" ) func main(){ s:= os.Args fmt.Println(s) } 直接执 ...

  9. Android之Activity

    Activity总结: Activity的顶层View是DecorView,而我们在onCreate函数中通过setContentView设置的View只不过是这个DecorView的一部分罢了.De ...

  10. jQuery-niceScroll使用中父子div都存在滚动条导致错位的问题

    1.做项目时因为客户要求改变了项目的整体样式,所以导致浏览器自带的滚动条样式与项目的样式风格格格不入,所以要使用一个滚动条插件来替换浏览器自带的滚动条,我在网上搜了下,发现niceScroll这个滚动 ...