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. 创建Car类,实例化并调用Car类计算运输的原料量是否足够

    package dx; public class Car { //构造类 public Car() { System.out.println("Car的构造类"); } //构造类 ...

  2. Hibernate-缓存

    Hibernate是一个持久层框架,经常访问物理数据库.为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在 ...

  3. Oracle 11g 默认用户名和密码

    安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下: 用户名 / 密码                      登录身份                              说明 ...

  4. 左偏树(DP)问题

    问题:A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ woul ...

  5. 人工蜂群算法-python实现

    ABSIndividual.py import numpy as np import ObjFunction class ABSIndividual: ''' individual of artifi ...

  6. BZOJ2654 tree

    Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...

  7. ZOJ 2110 Tempter of the Bone

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

  8. nginx&apache比较

    1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...

  9. RSA算法小记

    学习来源:http://www.cnblogs.com/vamei/p/3480994.html 小记: 一.数学基础: 欧拉Phi函数:Φ(n)=总数(从1到n-1中与n互质的整数) (1)欧拉定理 ...

  10. mysql 服务无法启动 服务没有报告任何错误

    问题 解决方法 1.必须保证 mysql 下不存在 data 文件夹,如果存在 data 文件夹,则先删除 mysql 下的 data 文件夹,然后初始化 mysqld --initialize 服务 ...