动态规划-最长可互除子序列 Largest Divisible Subset
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的更多相关文章
- 【leetcode】368. Largest Divisible Subset
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- 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. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- LeetCode "Largest Divisible Subset" !
Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...
- 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 ...
- [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- Largest Divisible Subset -- LeetCode
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
随机推荐
- rabbitmq_坑
一.None of the specified endpoints were reachable 这个异常在创建连接时抛出(CreateConnection()),原因一般是ConnectionF ...
- OAuth 白话简明教程 2.授权码模式(Authorization Code)
转自:http://www.cftea.com/c/2016/11/6703.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...
- GCC编译器ABI
ABI与EABI 1)ABI(Application Binary Interface for the ARM Architecture),描述了应用程序与cpu内核的低级接口. ABI允许编译好的目 ...
- 概率检索模型:BIM+BM25+BM25F
1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...
- Hdu dp
4856 这题说的是给了一个图 这个图有很多的隧道每个隧道是单向的 只能从一个入口进入从另一个入口出来 要求计算出走完这些隧道花的总时间 因为这个图是一个网格行的然后 先用bfs算出隧道的出口到每个隧 ...
- EditPlus 中文版停止更新
经过一段时间的考虑,我决定了终止 EditPlus 的中文版翻译工作. 感谢各位网友在这么长时间内对本汉化项目的关注.
- 测试开发-web测试要点
参数输入考虑 参数数值包含1个.多个.很多个.null.参数值前后包含空格的2种情况 数字类型:正数.负数.0.0.0.+0.0.-0.0.指数.对数.分数.小数.复数.科学计数法的测试,全角的数 ...
- javascript中的console.log有什么作用?
javascript中的console.log有什么作用? 主要是方便你调式javascript用的.你可以看到你在页面中输出的内容. 相比alert他的优点是:他能看到结构话的东西,如果是alert ...
- exp9《网络对抗》web安全基础实践201453331魏澍琛
201453331魏澍琛web安全基础实践 一.实验过程 1.webgoat开启 2.Injection Flaws练习 Command Injection 原网页中没有注入的地方,那就用burpsu ...
- 20145339 Exp5 MS11_050
20145339 Exp5 MS11_050 实验过程 使用命令msfconsole命令进入控制台 使用命令search ms11_050查看针对MS11_050漏洞的攻击模块 确定相应模块名之后,我 ...