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]

Credits:
Special thanks to @Stomach_ache for adding this problem and creating all test cases.

Analysis:

Sort the array, then for nums[i], if nums[i] % nums[j] == 0, then nums[i] is in the largest divisible set of nums[j]. We just need to find out the largest subset of nums[i].

Solution:

 public class Solution {
public List<Integer> largestDivisibleSubset(int[] nums) {
List<Integer> resList = new LinkedList<Integer>();
if (nums.length == 0) return resList; Arrays.sort(nums);
int[] size = new int[nums.length];
int[] pre = new int[nums.length]; int maxSize = 0;
int maxInd = -1; for (int i=0;i<nums.length;i++){
int localMax = 0;
int localInd = -1;
for (int j=0;j<i;j++)
if (nums[i] % nums[j] == 0 && localMax < size[j]+1){
localMax = size[j]+1;
localInd = j;
}
if (localInd == -1){
localMax = 1;
localInd = -1;
}
size[i] = localMax;
pre[i] = localInd; if (maxSize < localMax){
maxSize = localMax;
maxInd = i;
}
} resList.add(nums[maxInd]);
int preInd = pre[maxInd];
while (preInd != -1){
maxInd = preInd;
preInd = pre[maxInd];
resList.add(nums[maxInd]);
}
Collections.reverse(resList); return resList;
}
}

LeetCode-Largest Divisble Subset的更多相关文章

  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 Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  4. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

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

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

  6. Leetcode 368. Largest Divisible Subset

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

  7. 【leetcode】368. Largest Divisible Subset

    题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...

  8. Largest Divisible Subset -- LeetCode

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

  9. [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset

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

  10. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. div紧靠浏览器底部

    <body> <div class='bottom'> 这个div紧贴浏览器底部,且居中显示 </div> </body> css代码: .bottom ...

  2. Android SQLite数据储存方式

    SQLiteOpenHelper 类 用SQLiteOpenHelper 类中的 getWritableDatabase()和getReadableDatabase()方法可以获得数据库的引用. 为了 ...

  3. GET POST 区别详解

    Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据. 2. Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而 ...

  4. javascritp日期函数总结

    getDate()与getDay()的区别: obj.getDate()返回一个月中的某一天:obj.getDay()返回一个星期中的某一天. getYear()与getFullYear()的区别: ...

  5. PHP+ajax聊天室源码!支持长轮循跟定时请求两种

      var lastID = "1";//声明上次取回的消息的ID var isposted = false; var mGetTime;//设置setTimeout的返回值 // ...

  6. 关于VS2012下安装破解文件Visual Assit X的一点说明

    今天在使用Visual Studio 2012的时候,编写代码的助手Visual Assit X突然提示我说,试用期已过,要求我输入一个注册码,我靠,这货不是几个月前已经破解了吗,怎么今天傻不愣登的提 ...

  7. C#连接mysql实例

    using System; using System.Configuration; using MySql.Data.MySqlClient; /// <summary> /// Test ...

  8. JavaWeb之Servlet:请求 与 响应

    1 引入 浏览器和服务器的种类都有很多,要在它们之间通讯,必定要遵循一定的准则,而http协议就是这样的一个"准则". Http协议:规定了 浏览器 和 服务器 数据传输的一种格式 ...

  9. View和监听器

    View的基本概念 View就是Activity当中显示出来的控件,用对象来表示,如文本框的TextView类,按钮的Button类等等 每一种控件都对应一个类,都属于View的子类 在Activit ...

  10. 学习KMP算法

    int kmp(char * t,int lenT,char * pat,int lenPat){ ,posT=; int[] f=partialMatch(pat,lenPat)//获取pat字符串 ...