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. CF762D Maximum Path

    题目戳这里. 首先明确一点,数字最多往左走一次,走两次肯定是不可能的(因为只有\(3\)行). 然后我们用\(f_{i,j}\)表示前\(i\)行,第\(i\)行状态为\(j\)的最优解.(\(j\) ...

  2. recycleview的基础Adapter

    .封装了一个基础的adapter.,用于recycleview的快捷使用有BaseAdapter,BaseViewHolder,PAdapter,MainActivity public abstrac ...

  3. jquery学习总计

    1,jquery的基础语法 $(selector).action(); 选择器(selector)查询和查找html元素,action()执行对函数的操作. 2.选择器 id,类,类型,属性,属性值等 ...

  4. 汕头市队赛 SRM16

    T3 C-2 SRM 16 描述 给一个数列,给出两种数字, 询问在多少个非空区间中这两种数字出现次数相同. 输入格式 第一行:一个数字n,q,n表示数列长度,q表示q组询问 第二行n个数字表示数列A ...

  5. 阶段性总结⓵触摸事件&手势识别⓶Quartz2D绘图⓷CALayer图层⓸CAAnimation⓹UIDynamic UI动力学⓺KVC&KVO

    知识点复习   1. 触摸事件&手势识别   1> 4个触摸事件,针对视图的 2> 6个手势识别(除了用代码添加,也可以用Storyboard添加)   附加在某一个特定视图上的, ...

  6. 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)

    Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...

  7. [Leetcode Week10]Minimum Time Difference

    Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...

  8. 1、使用Xcode修改iOS项目工程名和路径名

    http://blog.sina.com.cn/s/blog_a42013280101blxo.html 对,好:错,改正. ------ 前言 系统 10.7 狮子 开发平台 xcode 4.5.2 ...

  9. Restful接口设计

    URL设计规范:/模块/资源/{标示}/集合1/... eg: /user/{uid}/friends ->好友列表 例子:秒杀系统API设计 1.请求参数绑定:@PathVariable(&q ...

  10. 关于测试url传值的问题

    url1:http://localhost:8080/fms/finan/isRiskCustomer.action?customername="xxxxxxxx"; 如上这样写, ...