【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 ...
随机推荐
- iOS开发——数据持久化OC篇&plist文件增删改查操作
Plist文件增删查改 主要操作: 1.//获得plist路径 -(NSString*)getPlistPath: 2.//判断沙盒中名为plistname的文件是否存在 -(BOOL ...
- MYSQL 博客
DavidYang的博客 - CSDN.NET DimitriK's (dim) Weblog Xaprb · Stay Curious! 飞鸿无痕的博客 - ChinaUnix博客 何登成的技术博客 ...
- android之存储篇——SQLite数据库
转载:android之存储篇_SQLite数据库_让你彻底学会SQLite的使用 SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在In ...
- Java再学习——栈(stack)和堆(heap)
一.内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们 ...
- LeetCode4 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 在提交SVN时有时候会报svn is already locked 错误
svn is already locked 解决方案: 如题所述经常在更新代码的时候会产生这样的问题!并且在对应的目录上操作Clean Up 没有任何的效果!如下解决方法. 在出错文件夹下,鼠标右键T ...
- jQery无缝滚动效果
思路: 赋值所有li,添加到ul末尾,重新计算ul宽度 每次移动一个固定的值,当超出一半时,将ul拉回原位 以下代码 <!DOCTYPE html> <html> <he ...
- 关于Google Chorme中字体小于12px的问题
问题:当字体大小设置成小于12px时,Google chrome中字体的大小始终显示为12px. 而其他浏览器则没有这个问题. 这时只需要在要改变字体大小的元素中添加 -webkit-transfor ...
- 2013年arcgis培训
关于开展“GIS空间分析及应用案例解析”培训班的通知 各企事业单位: 随着信息技术的发展,地理信息系统(简称GIS)产业异军突起,在国民经济各个行业中的应用日益广泛,物联网.智慧地球.3S技术等等 ...
- 【阿里云产品公测】一句话告诉你什么样的人该使用ACE,如何使用ACE
作者:阿里云用户小鸡咕咕 首先回应标题,这一句话就是:看完这篇帖子你就知道了. 前言 写在文章之前,我想先阐述一下写这篇文章的意义.可能大伙就要说了,写这篇文章不就是为了200的代金券吗?错, ...