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总的的边数减去重叠的 ...
随机推荐
- C#之接口
接口类Interface1.cs using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- svn error
svn: E205009: Local, non-commit operations do not take a log message or revision properties svn copy ...
- adobe dreameaver cs5 禁止更新
需要修改系统的host文件,将官方验证服务器全指向本机 用记事打开 C:\WINDOWS\system32\drivers\etc 下面的 host (没扩展名) 然后在后面添加 127.0.0.1 ...
- 关于.dll' could not be found 的问题以及解决方案
最近做项目遇到这个问题,搞了半天,也没有解决. 后台看看了生成的详细信息,提示引用的版本不统一,最后移除,再添加一个相同版本的Mysql.Data.Mysql.Data.Entity.EF6 就解决了 ...
- [HttpPost]和[AcceptVerbs(HttpVerbs.Post)]区别
1.共同点:[HttpPost]和[AcceptVerbs(HttpVerbs.Post)]都是只接受POST请求过来的数据. 2.不同点:在MVC中如果想一个action既可以回应POST请求也可以 ...
- Kinect开发资源汇总
Kinect开发资源汇总 转自: http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=254&highlight=kinec ...
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- Subsonic使用中
使用中,遇到各种奇葩问题,依依汇总. 1.引用了Subsonic层后,一运行就开始报错,提示未能找到文件!! //引用后,目标框架可能会被改变,subsonic的默认框架是2.0,请检查框架是否 ...
- C和指针 第十七章 二叉树删除节点
二叉树的节点删除分为三种情况: 1.删除的节点没有子节点,直接删除即可 2. 删除的节点有一个子节点,直接用子节点替换既可以 3.删除的节点有两个子节点. 对于第三种情况,一般是不删除这个节点,而是删 ...
- 百度地图多点路径加载以及调整页面js
$(document).ready(function () { /*用正则表达式获取url传递的地址参数,split后获得地址数组*/ bmap = new BMap.Map('mapcontaine ...