Leetcode 368.最大整除子集
最大整除子集
给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0。
如果有多个目标子集,返回其中任何一个均可。
示例 1:
输入: [1,2,3]
输出: [1,2] (当然, [1,3] 也正确)
示例 2:
输入: [1,2,4,8]
输出: [1,2,4,8]
解题分析:
如果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即可。
import java.util.*; class Solution {
public static List<Integer> largestDivisibleSubset(int[] nums) {
List<Integer> result=new ArrayList<Integer>();
List<Integer> numsList=new ArrayList<Integer>();
for(int i=0;i<nums.length;i++){
numsList.add(nums[i]);
}
if(nums.length==0) return result;
Collections.sort(numsList,Collections.reverseOrder());
int len=numsList.size();
int curMax=1;
int k=0;
int[] par=new int[len];
int[] dp=new int[len];
Arrays.fill(dp,1);
for(int i=0;i<len;i++){
par[i]=i;
}
for(int i=1;i<len;i++){
for(int j=0;j<i;j++){
if(numsList.get(j)%numsList.get(i)!=0){
continue;
}
if(dp[i]<dp[j]+1){
par[i]=j;
dp[i]=dp[j]+1;
}
if(dp[i]>curMax){
curMax=dp[i];
k=i;
}
}
}
while(par[k]!=k){
result.add(numsList.get(k));
k=par[k];
}
result.add(numsList.get(k));
return result;
}
}
Leetcode 368.最大整除子集的更多相关文章
- Java实现 LeetCode 368 最大整除子集
368. 最大整除子集 给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0. 如果有多个目标子集, ...
- 368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...
- 【JavaScript】Leetcode每日一题-最大整除子集
[JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...
- [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- LeetCode 368
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- [leetcode]78. Subsets数组子集
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- LeetCode刷题4——子集
一.要求 二.知识点 1.回溯算法 回溯算法相当于穷举法加剪枝,回溯算法总是和深度优先同时出现的,采用深度优先策略回溯到根,且根节点的所有子树都被搜索一遍才结束,并剪掉不符合要求的结果 三.解题思路 ...
随机推荐
- Windows7获取、更换桌面背景,C#
使用的API原型是 BOOL SystemParametersinfo(UINT uiAction,UINT uiParam,PVOID pvParam,UINT fWinlni); 在C#中定义如下 ...
- VUE注意事项(建项目)
1>删除空格影响的:删除掉框中的代码 2>不需要新建,直接打开APP.vue,在此文件上进行修改,(注意:index.html最好不要进行修改) 3>修改APP.vue为自己需要的页 ...
- How to detect the presence of the Visual C++ 2010 redistributable package
Question: I have seen your previous blog posts that describe how to detect the presence of the Visua ...
- webstorm使用总结
1.webstorm显示ES6语法错误,和nodejs语法错误未提示的问题,只需要在 此处解决ES6语法错误问题: 此处解决不支持node语法的问题: 然后就显示正常啦.
- moment.js获取当前日期是当年的第几周
/** * 实现当前日期是当年的第几周,再向前和向后推几周 * js数组保存当前日期的前后两周(共五周的数据) * */ var initSearchMajorChanges = function() ...
- 分享一个WebGL开发的网站-用JavaScript + WebGL开发3D模型
这张图每位程序员应该都深有感触. 人民心目中的程序员是这样的:坐在电脑面前噼里啪啦敲着键盘,运键如飞. 现实中程序员是这样的:编码5分钟,调试两小时. 今天我要给大家分享一个用WebGL开发的网站,感 ...
- Easier Done Than Said?(应用到的知识)
memset(b,0,sizeof(b)) 对于大块儿内存的分配,例如int arr[100];定义了数组arr,包含100个元素,如果你写成int arr[100]=0;想将数组全部内容初始化为0, ...
- CAD交互绘制批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment( pCustomEntity, pWorldDraw, curPt) { // 得到绘制参数. ...
- Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameter 标签: 构造器注入Spring
问题:要么是因为构造方法改变了,要么就是构造方法入参实例化失败(比如没有实现) 问题 在练习spring构造器注入方式的小程序的时候报错: Exception in thread “main” org ...
- HTML网页的浏览器标题栏显示小图标的方法
HTML网页的浏览器标题栏显示小图标的方法 就像这种效果,方法其实很简单,就是 在head头部里写: <link rel='icon' href='pic.ico ' type='image ...