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. Echarts使用及动态加载图表数据 折线图X轴数据动态加载

    Echarts简介 echarts,缩写来自Enterprise Charts,商业级数据图表,一个纯JavaScript的图表库,来自百度...我想应该够简洁了 使用Echarts 目前,就官网的文 ...

  2. UVM中的sequence使用(一)

    UVM中Driver,transaction,sequence,sequencer之间的关系. UVM将原来在Driver中的数据定义部分,单独拿出来成为Transaction,主要完成数据的rand ...

  3. 持续集成之二:搭建SVN服务器(SvnAdmin)

    安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 apache-tomcat-7.0.90 mysql-5.7. ...

  4. Keepalived保证Nginx高可用配置

    Keepalived保证Nginx高可用配置部署环境 keepalived-1.2.18 nginx-1.6.2 VM虚拟机redhat6.5-x64:192.168.1.201.192.168.1. ...

  5. python练习题,写一个方法 传进去列表和预期的value 求出所有变量得取值可能性(例如list为[1,2,3,4,5,6,12,19],value为20,结果是19+1==20只有一种可能性),要求时间复杂度为O(n)

    题目:(来自光荣之路老师)a+b==valuea+b+c=valuea+b+c+d==valuea+b+c+d+...=valuea和b....取值范围都在0-value写一个方法 传进去列表和预期得 ...

  6. python-正则表达式练习题

    1.匹配一行文字中的所有开头的字母内容 #coding=utf-8 import re s="i love you not because of who you are, but becau ...

  7. 计算概论(A)/基础编程练习1(8题)/7:奇数求和

    #include<stdio.h> int main() { // 输入非负整数 int m, n; scanf("%d %d", &m, &n); / ...

  8. API和正则表达式

    第一章 String & StringBuilderString类用类final修饰,不能被继承,String字符串被创建后永远无法被改变,但字符串引用可以重新赋值,改变引用的指向java字符 ...

  9. web.xml配置详解之listener

    web.xml配置详解之listener 定义 <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...

  10. this逃逸

    首先,什么是this逃逸? this逃逸是指类构造函数在返回实例之前,线程便持有该对象的引用. 常发生于在构造函数中启动线程或注册监听器. eg: public class ThisEscape { ...