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 ...
随机推荐
- 二十二、OGNL的一些其他操作
二十二.OGNL的一些其他操作 投影 ?判断满足条件 动作类代码: ^ $ public class Demo2Action extends ActionSupport { public ...
- java生成4个不同的随机数
package lianxi; import java.util.Random; public class suijishu { public static void main(String[] ar ...
- 举例详解CSS中的的优先级
计算优先级 优先级是根据由每种选择器类型构成的级联字串计算而成的.他是一个对应匹配表达式的权重. 如果优先级相同,靠后的 CSS 会应用到元素上. 注意:元素在文档树中的位置是不会影响优先级的优先级顺 ...
- JQuery Validate使用总结
本文参考了 http://www.cnblogs.com/linjiqin/p/3431835.html 可以在mvc 或webform项目中使用,可以方便快捷的对前端表单进行校验 一.导入两个js ...
- nginx 解决400 bad request 的方法(转载)
nginx的400错误比较难查找原因,因为此错误并不是每次都会出现的,另外,出现错误的时候,通常在浏览器和日志里看不到任何有关提示. 经长时间观察和大量试验查明,此乃request header过大所 ...
- 极路由1s手工安装ss插件
1.极路由申请开放root权限,将会失去保修,不过100块的东西还保修个什么 2.用putty连接极路由注意端口号 3. 然后一段一段执行下面的代码,很简单,复制一个段落的代码,粘贴进去,回车执行. ...
- 苹果Mac OS系统shell命令大全介绍
基本命令 1.列出文件 ls 参数 目录名 例: 看看驱动目录下有什么:ls /System/Library/Extensions 参数 -w 显示中文,-l 详细信息, -a 包括隐藏 ...
- Android开发之Source无法覆写public void onClick(View v)
初学Android开发,在为一个按钮[该按钮继承OnClickListener()]写监听时,发现无法在Source中引入public void onClick(View v),当时非常纳闷,平常情况 ...
- Linux 常见的进程调度算法
1.在介绍进程调度之前,先对进程的状态的概念应该有所了解,下面是关于进程状态的一些基本概念:进程的状态分为三种,分别为: 1).运行态:该状态表明进程在实际占用CPU 2).就绪态: 该状态下进程可以 ...
- ED/EP系列3《基本指令》
Ø --APPLICATIONBLOCK(应用锁定): Ø --APPLICATION UNBLOCK(应用解锁): Ø --CARDBLOCK(卡片锁定): Ø --EXTERNAL AUTHENT ...