LeetCode 368
题目描述:
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.
Example 1:
nums: [1,2,3] Result: [1,2] (of course, [1,3] will also be ok)
Example 2:
nums: [1,2,4,8] Result: [1,2,4,8]
思路:动态规划
需要注意到一点,题目要求的是整除,则当nums从小到大排列之后,对于i<j,如果 nums[k]%nums[j]==0 则一定有 nums[k]%nums[i] == 0
如此,该题目只需要维护一个一维数组即可得到结果。不过只是动归的话,仅能得到subset的大小,如果要得到subset的具体值,参照Dijkstra,再维护一个数组以记录last node,最后只需要回溯一下就OK。
AC代码:
public int[] sort(int [] nums){
int k, min;
for (int i = 0; i< nums.length; i++){
min = Integer.MAX_VALUE;
k = 0;
for (int j = i; j< nums.length; j++){
if ( min > nums[j]){
min = nums[j];
k = j;
}
}
nums[k] = nums[i];
nums[i] = min;
}
return nums;
}
public List<Integer> largestDivisibleSubset(int[] nums){
if (nums.length <= 0) return new ArrayList<Integer>();
nums = sort(nums);
int size = nums.length;
int [] times = new int[size];
int [] index = new int[size];
for (int i = 0; i< size; i++){
times[i] = 1;
index[i] = -1;
}
for (int i = 0; i< size; i++){
for (int j = 0; j< i; j++){
if (nums[i]%nums[j] == 0 && times[j] + 1 > times[i]){
times[i] = times[j] + 1;
index[i] = j;
}
}
}
int k = 0, max = 0;
for (int i = 0; i < size; i++){
if (max <= times[i]){
k = i; max = times[i];
}
}
List<Integer> res = new ArrayList<Integer>();
while(k >= 0){
res.add(nums[k]);
k = index[k];
}
return res;
}
经验&教训:
第一次我不是这样想的,第一次我是想,对于集合{a, b, c, d, e},先判断集合是否满足两两整除,如果不是,则分别考虑5个只有4个元素的子集,分别寻找子集中的最大子集。如此,复杂度为2^n。
主要是没有注意到,如果把集合排序,会有什么样的特性。有序数据真是创造奇迹的存在啊
LeetCode 368的更多相关文章
- Java实现 LeetCode 368 最大整除子集
368. 最大整除子集 给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0. 如果有多个目标子集, ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- Leetcode 368.最大整除子集
最大整除子集 给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0. 如果有多个目标子集,返回其中任 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 【leetcode】368. Largest Divisible Subset
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- .NET FRAMEWORK版本:4.0.30319; ASP.NET版本:4.6.118.0
https://gqqnbig.me/2015/11/23/net-framework%e7%89%88%e6%9c%ac4-0-30319-asp-net%e7%89%88%e6%9c%ac4-6- ...
- JS 小数的常用处理方法
1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...
- JavaScript数据存取的性能问题
JavaScript中四种基本的数据存取位置: 字面量:只代表自身 字符串.数字.布尔值.对象.函数.数组.正则,以及null和undefined 快 本地变量:var定义的 快 数组元素 ...
- Linux 命令
Linux 常用命令 su root 切换root用户 touch /etc/www/html/1.txt 创建文件 mkdir /usr/local/apache2 建立文件夹 rm -rf ...
- GIT文档
GIT文档http://git.oschina.net/progit/http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c ...
- 源码包---linux软件安装与管理
源代码推荐保存位置: /usr/local/src 软件安装位置: /usr/local 如何确定安装过程报错: 安装过程停止 并出现error / warning / no 的提示 ./config ...
- windows server 2012 使用问题
1.端口映射,把宿主机的端口映射到hyper-v创建的虚拟机上 访问宿主机的公网IP的某个端口,就等于访问这个虚拟机上的端口 具体实现: 在宿主机上命令行输入 添加一个端口映射 netsh inter ...
- Maven把自己的包部署到远程仓库
1,配置项目的POM文件 <dependencyManagement> </dependencies> </dependency> ...... </depe ...
- Fedora下依赖库的解决
转载于http://blog.sina.com.cn/s/blog_6f74890d0101dpp4.html x86_64版本的fedora中打开共享对象文件失败的解决小技巧———以qq for l ...
- bind绑定参数
const curry = (fn) => (...args)=>fn.bind(null,...args); const split = curry((splitOn, str) =&g ...