【leetcode】368. Largest Divisible 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.
解题分析:
如果a%b==0,则a=mb,所以如果把数组排序后如果a%b==0,且b%c==0则a%c==0。这就为用动态规划实现提供了可能性。设置一个数组result,result[i]表示i出包含的满足条件的子集个数。则如果nums[i]%nums[j]==0,则result[i]=result[j]+1;同时由于函数要返回的是一个List,所以我们要保存最长集合的路径。这个功能可以通过设置一个pre数组保存能被nums[i]整除的上一个数的索引。并在保存max值的同时保存max所在的位置maxIndex即可。
public class Solution {
public static List<Integer> largestDivisibleSubset(int[] nums) {
if(nums.length==0){
return new ArrayList<Integer>();
}
if(nums.length==1){
List<Integer> array = new ArrayList<Integer>();
array.add(nums[0]);
return array;
}
Arrays.sort(nums);
int len = nums.length;
int[] result=new int[len];
int[] pre=new int[len];
result[0]=nums[0];
pre[0]=-1;
int max=1;
int maxIndex=0;
for(int i=1;i<nums.length;i++){
result[i]=1;
pre[i]=-1;
for(int j=0;j<i;j++){
if(nums[i]%nums[j]==0){
result[i]=result[j]+1;
pre[i]=j;
if(result[i]>max){
max=result[i];
maxIndex=i;
}
}
}
}
List<Integer> array = new LinkedList<Integer>();
int index = maxIndex;
while(index!=-1){
array.add(0,nums[index]);
index=pre[index];
}
return array;
}
}
【leetcode】368. Largest Divisible Subset的更多相关文章
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【leetcode】1090. Largest Values From Labels
题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
随机推荐
- excel VLOOKUP函数的用法
VLOOKUP函数是Excel中几个最重函数之中的一个,为了方便大家学习,兰色幻想特针对VLOOKUP函数的使用和扩展应用,进行一次全面综合的说明.本文为入门部分 一.入门级 VLOOKUP是一个查找 ...
- 利用URLScan工具过滤URL中的特殊字符(仅针对IIS6)
客户公司搞安全检查,扫描出来我们之前做的系统有一个高危漏洞:IIS tilde directory enumeration,也就是利用“~”字符猜解暴露短文件/文件夹名,比如,采用这种方式构造URL: ...
- Ubuntu下VSFTPD(五)(匿名FTP设置方法)
匿名FTP设置方法: 通常在登录FTP服务器的用户不确定的情况下,应将FTP服务器设置为允许匿名账号登录的FTP服务器 1.启用匿名帐号 anonymous_enable=YES local_ ...
- c#读写ini配置文件示例
虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧 其他人写的都是调用非托管kernel32.dll.我也用过 ...
- Long Long Message 后缀数组入门题
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 22564 Accepted: 92 ...
- 1.4.2 solr字段类型--(1.4.2.6)使用外部文件和程序
1.4.2 solr字段类型 (1.4.2.1) 字段类型定义和字段类型属性. (1.4.2.2) solr附带的字段类型 (1.4.2.3) 使用货币和汇率 (1.4.2.4) 使用Dates(日期 ...
- 通过改变uiview的layer的frame来实现进度条
#import <UIKit/UIKit.h> @interface ProgressView : UIView @property(nonatomic,assign)CGFloat pr ...
- Android小项目之十 应用程序更新的签名问题
------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...
- 懒人福利:Xcode插件将JSON格式化输出为模型的属性->ESJsonFormat-Xcode
这是一个直接将json数据转换为模型数据的插件,只需要在控制台输入json数据,就可以在模型文件的.h文件中生成对应的模型数据 对于模型套模型的数据也做了处理,比较方便. 有需要的人可以尝试一下,但不 ...
- DNS resolving 占用大量日志
公司内部DNS配置好后,测试解析正常,只是几乎每秒都有无法解析的日志产生.但分析日志时发现,每天的日志都有2G左右.日志中几乎全是类似network unreachable resolving 'ww ...