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. MyEclipse内存不足问题

    1.修改eclipse.ini 在Myeclipse安装目录下G:\MyEclipse8.5\Genuitec\MyEclipse 8.5有一个myeclipse.ini配置文件,设置如下: -vma ...

  2. 大概了解了flexbox

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 关于obj和基本类通过函数参数传进去执行是否改变原来的值

    var obj = { p1 : 1, p2 : 2 }; (function(_/* 这个东东是地址的应用哦 */){ _.p1 = 3, _.p2 = 4 })(obj) var i = 2; ( ...

  4. poj3522 kruskal+枚举

    题目的意思是求构成生成树的边的最大边和最小边的差最小.枚举即可 #include<stdio.h> #include<string.h> #include<algorit ...

  5. Spring控制Hibernate的缓存机制ehcache

    首先在spring.xml中进入bean <prop key="hibernate.cache.use_second_level_cache">true</pro ...

  6. bootstrap_UI

  7. 【Gym 100712B】Rock-Paper-Scissors

    题 题意 对给定的对手的出拳顺序,如果只能按几个R,然后几个P,再几个S的顺序出拳(几个也可以是0个),那么求赢的方法有多少种. 分析 我原来想枚举P开始的位置和S开始的位置然后算得分,但是超时了o( ...

  8. 【Matplotlib】 增加图例

    相关文档: Legend guide legend() command Legend API 控制图例入口 无参调用 legend() 会自动获取图例 handles 以及相关的 labels.其对应 ...

  9. Oracle导出导入数据库的方式

    一.导入导出.dmp文件 利用cmd的操作命令导出,详情如下(备注:方法二是转载网上的教程):1:G:\Oracle\product\10.1.0\Client_1\NETWORK\ADMIN目录下有 ...

  10. 萤火虫算法-python实现

    FAIndividual.py import numpy as np import ObjFunction class FAIndividual: ''' individual of firefly ...