LeetCode-Largest Divisble Subset
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的更多相关文章
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- LeetCode "Largest Divisible Subset" !
Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 【leetcode】368. Largest Divisible Subset
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- Largest Divisible Subset -- LeetCode
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- Sublime 3114 + 转换GBK方法
下载地址: http://files.cnblogs.com/files/wuyifu/Sublime_Text_Build_3114_x64_Setup.zip -– BEGIN LICENSE - ...
- 在xargx命令中如何使用重定向
ls *.txt | xargs -i -n 1 sh -c "cut -f 1-3 {} > ../{}"即可
- js,jquery获取下拉框选中的option
js获取select选中的值: var sel=document.getElementById("select1"); var index = sel.selectedIndex; ...
- Linux系统快速启动方案
========================= 基本常识 ========================= Linux系统基本启动流程: 1. CPU从ROM(如果有的 ...
- 【UEditor】远程上传图片到【七牛云存储】
杂谈:最近在玩一个第三方的微信开发平台,里面的图片都是上传到[七牛云存储]的,用了一下非常的好用,支持各种语言,SDK齐全.支持全分布式系统架构以及存储技术和数据加速,于是决定将网站的图片都存储到七牛 ...
- python简单的发送邮件
python 利用smtplib来发送邮件,具体的代码如下 一. 编辑smtp_v2.py vim /home/python/smtp_v2.py #!/usr/bin/env python # -* ...
- (六)、nodejs中的express框架获取http参数
express获取参数方法: 一.通过req.params app.get('/user/:id', function(req, res){ res.send('user ' + req.params ...
- winform 打包部署
1.使用VS 自带的打包工具,制作winform安装项目 开发环境:VS 2008 Access 操作系统:Windows XP 开发语言:C# 步骤: 第一步:打开开发环境VS2008,新建项目,选 ...
- jQuery实现跨域访问
示例: $.ajax({ url: url, crossDomain: true, async: false,dataType:"jsonp" }); 说明:$.ajax()有很多 ...
- freefilesync 7 使用
官方下载地址:http://www.freefilesync.org/download.php 1.打开FreeFileSync 设置左右的文件夹,设置过滤规则,设置同步方式(双向.单向),执行同步 ...