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: [1,2,3]

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

Example 2:

nums: [1,2,4,8]

Result: [1,2,4,8]

分析:

思路: 将数组排序,然后用dp[i]表示从0到i最大的集合。为了得到dp[i]的值, 我们从i - 1 到 0 看是否 nums[i] % nums[j] ==0,  如果是,dp[i] = max(dp[i], dp[j]+1), 因为数组按照降序排序, 所以nums[j] < nums[i],并且之前能够被nums[j]整除的数, 也必然能够被 nums[i]整除。

 public class Solution {
public List<Integer> largestDivisibleSubset(int[] nums) {
if (nums == null || nums.length == ) return new ArrayList<Integer>(); int n = nums.length;
Arrays.sort(nums);
int[] dp = new int[n];
Arrays.fill(dp, );
int[] parent = new int[n];
Arrays.fill(parent, -);//当parent数组中某数为-1时,表示这个数自己是一个集合
int max = , max_index = ;
for (int i = ; i < n; i++) { //calculate dp[i]
for (int j = i - ; j >= ; j--) { //i > j
if (nums[i] % nums[j] == && dp[i] < dp[j] + ) { //positive distinct numbers in num
dp[i] = dp[j] + ;
parent[i] = j;
if (dp[i] > max) {
max = dp[i];
max_index = i;
}
}
}
}
return genResult(nums, parent, max_index);
} public List<Integer> genResult(int[] nums, int[] parent, int max_index) {
List<Integer> result = new ArrayList<>();
int iter = max_index;
while (iter != -) {
result.add(nums[iter]);
iter = parent[iter];
}
return result;
}
}

Largest Divisible Subset的更多相关文章

  1. LeetCode "Largest Divisible Subset" !

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

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

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

  3. Leetcode 368. Largest Divisible Subset

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

  4. 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 -- LeetCode

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

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

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

  9. 368. Largest Divisible Subset

    class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { vec ...

随机推荐

  1. Cas_Server端安装

        一.Cas Server版本:3.5.2 下载地址:http://download.csdn.net/detail/xiaohuzi1987/5262980   二.安装步骤: 1.解压cas ...

  2. hdu3535 混合背包

    分三种情况. 至少取一种 那可以直接取 或者从上一种情况来取.dp[i][k]=max(dp[i][k],dp[i-1][k-a[j].c]+a[j].v,dp[i][k-a[j].c]+a[j].v ...

  3. Java-日期转换

    如下: package 时间日期类; import java.text.SimpleDateFormat; import java.util.Date; public class 日期格式转换 { / ...

  4. Java异常分类

    一.基本概念 看java的异常结构图 Throwable是所有异常的根,java.lang.ThrowableError是错误,java.lang.ErrorException是异常,java.lan ...

  5. BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)

    Va爷的胡策题T2 E. Fairy time limit per test1.5 seconds memory limit per test256 megabytes inputstandard i ...

  6. BZOJ1034 [ZJOI2008]泡泡堂BNB

    Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表 队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对 ...

  7. 神经网络训练中的Tricks之高效BP(反向传播算法)

    神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...

  8. WINDOWS之入侵痕迹清理总结

    Windows的日志文件通常有应用程序日志,安全日志.系统日志.DNS服务器日志.FTP日志.WWW日志等等. 应用程序日志文件:%systemroot%\system32\config\AppEve ...

  9. Linux中如何查看文件的最初创建时间

    查看 一个文件的 最初创建时间: Linux中如何查看文件的最初创建时间  linux     目前Linux没有直接查看创建文件的命令,你只能通过文件是否被修改过来进行判断. //查看代码stat  ...

  10. linux利用grep查看打印匹配的下几行或前后几行的命令

    转自:http://www.itokit.com/2013/0308/74883.html linux系统中,利用grep打印匹配的上下几行   如果在只是想匹配模式的上下几行,grep可以实现.   ...