2018-08-28 17:51:04

问题描述:

问题求解:

本题是一个求最优解的问题,很自然的会想到动态规划来进行解决。但是刚开始还是陷入了僵局,直到看到了hint:LIS,才有了进一步的思路。下面是最初的一个解法。使用的是map来记录信息。

    public List<Integer> largestDivisibleSubset(int[] nums) {
if (nums.length == 0) return new ArrayList<>();
Arrays.sort(nums);
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
List<Integer> tmp = null;
int maxlen = 0;
for (int j = 0; j < i; j++) {
if (nums[i] % nums[j] == 0 && maxlen < map.get(nums[j]).size()) {
tmp = new ArrayList<>(map.get(nums[j]));
maxlen = tmp.size();
}
}
if (tmp == null) tmp = new ArrayList<>();
tmp.add(nums[i]);
map.put(nums[i], tmp);
}
int maxlen = 0;
List<Integer> res = null;
for (Integer i : map.keySet()) {
if (map.get(i).size() > maxlen) {
res = map.get(i);
maxlen = res.size();
}
}
return res;
}

当然上述的代码效率不是很高,我们可以使用两个数组来进行维护。

    public List<Integer> largestDivisibleSubset(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums.length == 0) return res;
Arrays.sort(nums);
int[] dp = new int[nums.length];
int[] prev = new int[nums.length];
int max = 0;
int index = -1;
for (int i = 0; i < nums.length; i++) {
prev[i] = -1;
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] % nums[j] == 0 && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
if (dp[i] > max) {
max = dp[i];
index = i;
}
}
while (index != -1) {
res.add(nums[index]);
index = prev[index];
}
return res;
}

动态规划-最长可互除子序列 Largest Divisible Subset的更多相关文章

  1. 【leetcode】368. Largest Divisible Subset

    题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...

  2. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  3. 【LeetCode】368. Largest Divisible Subset 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...

  4. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  5. LeetCode "Largest Divisible Subset" !

    Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...

  6. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  7. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. Largest Divisible Subset -- LeetCode

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

随机推荐

  1. kendo 级联加带搜索的下拉框以及js赋值

    1‘.js给下拉框赋值 $("#UserRole").data("kendoDropDownList").value(dataItem.RoleName); $ ...

  2. Java Nested Classes(内部类~第一篇英文技术文档翻译)

    鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...

  3. Intro to Python for Data Science Learning 6 - NumPy

    NumPy From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=1 ...

  4. Asp.net MVC 通过自定义ControllerFactory实现构造器注入

    一.重写ControllerFactory的GetControllerInstance ControllerFactory是asp.net中用于在运行时构造Controller的工厂 ,默认使用的工厂 ...

  5. linux常用命令:yum 命令

    用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级. 1.命令格式:    yum  [参数] [软件名]2.命令功能:    功能:  yum提供了查找.安装.删除某一个.一组甚至全 ...

  6. node.js cookie session使用教程

    众所周知,HTTP 是一个无状态协议,所以客户端每次发出请求时,下一次请求无法得知上一次请求所包含的状态数据,如何能把一个用户的状态数据关联起来呢? cookie 首先产生了 cookie 这门技术来 ...

  7. Linux基础命令---swapon

    swapon 在指定的设备上启用交换分区,使用的设备或文件由专用文件参数提供.它可以是”-L label”或”-U UUID”,以指示一个设备的标签或UUID.对swapon的调用通常发生在系统引导脚 ...

  8. 左连接LEFT JOIN 连接自己时的查询结果测试

    #左连接LEFT JOIN 连接自己时的查询结果测试 #左连接LEFT JOIN 连接自己时的查询结果(都会出现两个重复字段),两个表都有as后只能查询相等条件merchant_shop_id非nul ...

  9. SQLServer 进阶记录式学习

    1.强制类型转换  nvarchar->decimal ) , , ) SET @i = '1083.589' SET @num = @i SELECT @num , )) SELECT @nu ...

  10. P3008 [USACO11JAN]道路和飞机Roads and Planes

    P3008 [USACO11JAN]道路和飞机Roads and Planes Dijkstra+Tarjan 因为题目有特殊限制所以不用担心负权的问题 但是朴素的Dijkstra就算用堆优化,也显然 ...