【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开发——动画编程Swift篇&(五)CAKeyframeAnimation
CAKeyframeAnimation //CAKeyframeAnimation-关键针动画 @IBAction func cakFly() { let animation = CAKeyframe ...
- 项目源码--JAVA基于LBS地理位置信息应用的服务端
技术要点: 1. LBS应用框架服务端实现 2. JAVA服务端技术 3. MYSQL数据库技术 4. 源码带详细的中文注释 ...... 详细介绍: 1. LBS应用框架服务端实现 此套源码是基 ...
- linux man使用方法 和centos安装中文man包 --转
http://blog.chinaunix.net/uid-25100840-id-302308.html 这两天学习<linux设备驱动程序开发详解>中的异步通知,其中有一个fcntl( ...
- 计算圆周率 Pi (π)值, 精确到小数点后 10000 位 只需要 30 多句代码
大家都知道π=3.1415926……无穷多位, 历史上很多人都在计算这个数, 一直认为是一个非常复杂的问题.现在有了电脑, 这个问题就简单了.电脑可以利用级数计算出很多高精度的值, 有关级数的问题请参 ...
- AllJoyn Bundled Daemon 使用方式研究
关于AllJoyn不多做介绍,请看官网:www.alljoyn.org/ 0. 问题来源: 应用程序要使用AllJoyn库,就必须启动deamon. 目前有两种方式: 使用standalone形式,单 ...
- Mac 下抓包工具 Charles 修改特定请求
Charles 是 Mac 下常用的抓包工具,它通过将自己设置成系统的代理服务器,从而完成了抓包的工作. 同类的工具还有 fiddler 和 Wireshark,其中 fiddler 是微软公司使用 ...
- 函数参数选项的处理getopt getopt_long getopt_long_only
转载:http://blog.chinaunix.net/uid-20321537-id-1966849.html 在头文件中int getopt(int argc,char *argv[], c ...
- Ubuntu/Debian 安装lxml的正确方式
lxml是Python的一个库,主要用于处理XML和HTML. 最近需要用lxml,但是在Ubuntu上直接pip安装失败,研究了半天终于找到了正确安装方法,记录在此. 由于Ubuntu和Debian ...
- Jax-ws开发实例
初次接触Jax-ws(Java API xml web Service)感觉挺简单的,在这里写下我的所学的: 大概的顺序是:首先定义接口 ,然后写接口的实现类,最后编写客户端.步骤是挺简单的,来看看代 ...
- html 中根据后台参数显示 相应的样式 EL表达式
<li><a class="${return_product_statu==-1||return_product_statu==null?'switch_xz':'none ...