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.回溯算法 回溯算法相当于穷举法加剪枝,回溯算法总是和深度优先同时出现的,采用深度优先策略回溯到根,且根节点的所有子树都被搜索一遍才结束,并剪掉不符合要求的结果 三.解题思路 ...
随机推荐
- 前端开发神器 - Brackets
做了几年的 .Net 项目开发,后来公司转 Java 语言开发,Java 做了还没一年,公司准备前后端分离开发,而我被分到前端! Brackets是一款基于web(html+css+js)开发的web ...
- [Ubuntu]“ubuntu.sh: 113: ubuntu.sh:Syntax error: "(" unexpected ”报错解决方法
原因:有可能是兼容性问题 解决方法: 1.sudo dpkg-reconfigure dash 2.在弹出的窗口选择no
- SQL 基本编程
定义变量 赋值 取值 分支语句 循环语句 定义变量 declare @变量 数据类型 //@必须带着 不然程序不知道变量是什么 不带@ 电脑会报错 例如 declare ...
- strophe.js 插件 XMPP openfire
参考资料:http://strophe.im/strophejs/ https://github.com/strophe/strophejs-plugins http://amazeui.org/ 最 ...
- POJ 3162 Walking Race (树的直径,单调队列)
题意:给定一棵带边权的n个节点的树,首先要求出每个点的最长路,然后写成序列d[1],d[2]...d[n],然后求满足 区间最大值-区间最小值<=k 的最大区间长度为多少? 思路: 分两步进行: ...
- (转)SpringMVC学习(三)——SpringMVC的配置文件
http://blog.csdn.net/yerenyuan_pku/article/details/72231527 读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后 ...
- Xcode编译工具
一.关于Other Linker Flags xcode中,在“Targets”选项下有Other Linker Flags选项,在这里可以填写xcode链接器的参数,如:-ObjC.-all_loa ...
- python_112_断言
#断言 如果满足断言的执行程序,如果不满足则抛错误 assert type(1) is int print('断言正确的话,就继续执行') # assert type('a') is int #Ass ...
- python_112_网络编程 Socket编程
实例1:客户端发小写英文,服务器端返回给客户端大写英文(仅支持一次接受发送) 服务器端: #服务器端(先于客户端运行) import socket server=socket.socket() ser ...
- StringMVCWeb接受前台值的几种方式
这些决定与request header 的Content-Type属性 1.通过@RequestParam @RequestParam Map<String, Object> pa ...