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 ...
随机推荐
- C# winform combobox控件中子项加删除按钮(原创)
效果如下图,本人网上搜索资料加上自己的研究终于实现了在combobox子项中加上删除按钮. 一.窗体中的代码: using System; using System.Collections.Gener ...
- jquery ajax跨域请求详解
本文章来给大家详细jquery中的ajax跨域请求, 在JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式.分别是JQuery的jquery.ajax jsonp格式和jque ...
- 重回程序员之路。重写博客。我的ecshop小京东二开问题汇总与解决方案。
问题1:混合支付(余额支付+在线支付)不跟更新订单状态问题. 解决方案:http://bbs.ecshop.com/viewthread.php?tid=156761&highlight= i ...
- 编写高质量代码改善C#程序的157个建议
1.使用StringBuilder或者使用string.Format("{0}{1}{2}{3}", a, b, c, d)拼接字符串. 2.使用默认转型方法,比如使用类型内置的P ...
- iPhone图标及启动画面大小 xcode5
启动画面 文件名 大小px Default.png 320*480 Default@2x.png 640*960 Default-568h@2x.png 640*1136 图标 文件名 大小px ...
- Objective-C关于数据处理
本文介绍如何在Objective-C中操作数据.我们将使用数组.指针.字符串等. 数组是数据项的一个集合,这些数据项叫做元素,我们可以用一个数组索引来引用元素.例如,如果把数字存储在一个名为array ...
- 菜鸟学习Struts——bean标签库
一.Struts标签库. Struts实际上包含了4个标签库:bean,logic,html,tiles bean:用来在属性范围中定义或取得属性的,同时可以读取资源文件信息 logic:替代JSTL ...
- Lex+YACC详解
1. 简介 只要你在Unix环境中写过程序,你必定会邂逅神秘的Lex&YACC,就如GNU/Linux用户所熟知的Flex&Bison,这里的Flex就是由Vern Paxon实现的一 ...
- Redis 四:存储类型之有序集合
有序集合似乎更大的操作是由于加了一个叫做“分子”的东西 事实上就好像普通的数据,只是为这个数据加了一个纯数字的标识, 通过操作这些标识来得到我们想要的数据! 分子可以是整形,也可以是双精度浮点型: = ...
- Android 实现子View的状态跟随父容器的状态
最近自学着做东西,需要做一个效果,就是我ListView的Item点击下或者选中的时候,我Item里面的各个组件的状态要和我Item的状态保持一直,这样我就可以用XML,去根据组件的不同状态去实现不同 ...